The head of Britain's digital espionage agency has apologized for the organization's historic prejudice against homosexuals, saying it failed to learn from the treatment of World War II codebreaker Alan Turing.

In a rare public speech, GCHQ chief Robert Hannigan told a gathering organized by the rights group Stonewall that the agency's ban on homosexuals had caused long-lasting psychological damage to many and hurt the agency because talented people were excluded from working there.

"The fact that it was common practice for decades reflected the intolerance of the times and the pressures of the Cold War, but it does not make it any less wrong and we should apologize for it," Hannigan said Friday at the conference organized by Stonewall, which campaigns for lesbian, gay, bisexual and transgender equality.

The speech offered a poignant tribute to Turing, the gay computer science pioneer and architect of the effort to crack Nazi Germany's Enigma cipher. Turing was convicted of indecency in 1952 and stripped of his security clearance. He later committed suicide.

A 2014 film about Turing, "The Imitation Game " starring Benedict Cumberbatch, brought his story to a new generation. At GCHQ, Turing is now seen as a genius-- "a problem-solver who was not afraid to think differently and radically," Hannigan said.

It was partly to honor Turing that the agency's headquarters was lit up during a global celebration of gender and sexual diversity last year.

"It was also kind of an act of atonement -- for the lost opportunity of his early death," Hannigan said. "Who knows what Turing would have gone on to do, where, for example, he might have taken his pioneering interest in artificial intelligence, which is the thing everyone is talking about. We will never know and should, as a society, never repeat that mistake."

Hannigan said things are different now.

To make the point, he shared a story about an internal agency blog headlined "So it's goodbye from him." Hannigan said that at first he thought it was written by someone who was leaving the agency for the private sector. It turned out to be the story of a transgender employee -- who he called Emma -- who had finally decided to start the process of transition.

"We have a lot of courageous staff, civilian and military, straight and gay, who have deployed to Afghanistan, to Iraq, and other conflicts...," Hannigan said. "But it takes a particular kind of courage to write what Emma wrote in front of thousands of her colleagues."

Hannigan said he was proud the blog was the most "liked" the agency had ever had, and that the comments were genuinely supportive. But he stressed that GCHQ was still far from a utopia.

"That is the real point of diversity for me," he said. "To do our job, which is solving some of the hardest technology problems the world faces for security reasons, we need all talents and we need people who dare to think differently and be different. ... Dull uniformity would completely destroy us."