SAN FRANCISCO – The downfall of Hewlett-Packard Co.’s CEO Mark Hurd was swift and stunning.

He abruptly resigned Friday over previously undisclosed allegations of sexual harassment and financial shenanigans involving a relationship with a former HP contractor.

But before the news broke and sent HP’s stock tumbling, Hurd, 53, had a nearly bulletproof reputation on Wall Street for his stewardship of the world’s biggest maker of personal computers and printers.

HP said that Hurd was forced out after the company discovered that he had a secret relationship with a woman who worked with HP on marketing matters, and that he falsified expense and other financial reports to conceal the relationship and help get the contractor paid for work she didn’t do.

Hurd said it was a “painful decision” to leave but acknowledged there were “instances in which I did not live up to the standards and principles of trust, respect and integrity that I have espoused at HP.” He will receive a $12.2 million severance payment.

The company’s chief financial officer, Cathie Lesjak, 51, was named interim CEO. She has been with HP for 24 years but has taken herself out of the running to fill the position permanently.

Mark Kelleher, an analyst with Brigantine Advisors, said Hurd’s resignation boils down to “one person doing some really stupid things.”

“That gave a great deal of comfort to investors that this was not a company-fundamental issue,” he said.

Beloved by investors for his relentless cost-cutting — and scorned by thousands of laid-off employees for the same — Hurd was seen as an outsider who rescued the company from the mess left behind by his ill-fated predecessor, Carly Fiorina.

He has transformed 71-year-old HP from a computer and printer maker hooked on profits from printer ink, into a company that looks a lot like its archrival IBM Corp. and is now a major player in technology services and other fast-growing areas.

Under Hurd, HP’s stock price doubled, its market value grew by more than $40 billion and HP has become the world’s No. 1 technology company by revenue.

On Friday, HP’s shares closed trading on the New York Stock Exchange at $46.30, then tumbled to $41.85 as investors reacted to the stunning news of his resignation.

Now, five years after taking the HP helm, Hurd is suddenly out of a job and his situation is similar to Fiorina’s in a key way: Both were forced out as the company stands ready to reap the benefits of sweeping changes they made that transformed the Silicon Valley institution in fundamental ways.

Fiorina was forced out in 2005 in the wake of a fight over her decision to acquire Compaq Computer and an upheaval over her personality and business strategies. The Compaq deal was bitterly divisive but proved instrumental in HP’s ascendance to the rank of the world’s top personal computer seller — something that happened under Hurd’s tenure.