HARRISBURG — A former Philadelphia police sergeant has been released after spending nearly five years in federal detention for failing to unlock encrypted hard drives on which investigators believed he was storing child pornography.

The U.S. Court of Appeals for the Third Circuit ruled 2-1 Thursday that federal law allows the government to hold someone for no longer than 18 months for civil contempt. The appeals court directed that Francis Rawls be set free.

Rawls had been in federal custody since September 2015 because investigators say he refused to hand over passwords to decrypt a pair of external hard drives they seized during a search of his home. Rawls has maintained that he does not know the passwords, according to his lawyer, Keith M. Donoghue with the Philadelphia-based Federal Community Defender Office.

The former sergeant’s case had attracted national attention from prosecutors, defense attorneys, and advocates including the Electronic Frontier Foundation for its potential ramifications on fundamental concepts including privacy, self-incrimination, and unreasonable search and seizure.

Donoghue said Rawls was released Thursday night, and faces the challenge of rebuilding his life outside jail. He was fired in 2015 after 17 years with the city police force.

“It's a daunting prospect,” Donoghue said.

A spokesperson for U.S. Attorney William McSwain’s office, asked if prosecutors intended to appeal the decision or whether Rawls might face criminal charges, said only that they are reviewing their options.

A federal district judge ordered in 2015 that Rawls be held in jail indefinitely and Rawls had lost an earlier appeal, in which the Third Circuit ruled that he could be compelled to unlock the devices because investigators had amassed enough evidence even without the passwords to prove that the encrypted files were illegal pornographic material. In its 2017 opinion, the court sidestepped the question of whether forcing him to do so violated Rawls’ Fifth Amendment rights against self-incrimination.

In its ruling Thursday, a different three-judge Third Circuit panel addressed a new question: whether Rawls’ continued incarceration for contempt violated federal laws capping at 18 months the amount of time witnesses could be jailed for refusing to testify.

The government argued those limits did not apply to Rawls and disputed characterizing him as a “witness.” He had simply been ordered — first by a search warrant, then by a series of courts — to provide access to his unencrypted hard drives, not provide testimony or new evidence against himself, prosecutors said.

The appellate panel disagreed.

“Rawls maintains a dual identity — Rawls is a witness for purposes of contempt, while a suspect for child pornography offenses," Judge Julio M. Fuentes wrote for the majority. “We find that [federal law] caps Rawls’ detention for committing civil contempt in his capacity as a material witness at 18 months.”

Judge Jane Richards Roth wrote in a dissent that the 18-month limit should not apply to Rawls because he is not a witness but rather someone who has not complied with a lawful search warrant.

“If Rawls should contend that he has forgotten his passwords, he is free to file a motion with the district court seeking his release on the ground that his confinement has ceased to be coercive and has therefore become punitive,” Roth wrote.

In a previous court filing, Rawls’ lawyers wrote that during a District Court hearing, a detective “testified it was his ‘best guess’ that child pornography would be found on the hard drives.”

They also said Rawls remained jailed “rather than waiving his right against self-incrimination by testifying to his inability to comply with the decryption order.”

The case against Rawls began in March 2015, when detectives with the Delaware County District Attorney’s Office identified him as a suspect in an investigation of a website called Freenet, a site that allegedly allowed users to share child pornography.

Investigators said data showed that devices owned by Rawls — referred to as “John Doe” in court documents — had been routing or requesting such files.

Authorities seized electronic devices from Rawls’ home, including an Apple Mac Pro computer on which they found one image depicting a pubescent girl in a sexually suggestive position and logs that suggested he had visited groups with titles common in child exploitation.

But they were unable to unlock the hard drives, which also were encrypted. The labeling of some files caused investigators to believe that the hard drives contained child pornography and Rawls’ sister reported he once accessed the drives in her presence, revealing that they contained hundreds of illicit videos and photos.

This article contains reporting from the Associated Press.