At least eight cops linked to the ongoing NYPD corruption scandal will get to keep their lucrative pensions, thanks to a little-known law signed by Gov. Andrew Cuomo four years ago.

Under that law, cops with more than 20 years of service who file for retirement are guaranteed their pensions, as well as about $12,000 more annually in a yearly stipend, unless they’re convicted of a felony while actually still on the NYPD payroll.

The annual lifetime payouts will be near six figures or even higher, as they are based on half of the cops’ salaries with an additional fractional increase for each year worked past 20 years.

Before the law changed, officers who worked past 20 years risked their pensions — unlike cops elsewhere in New York state — if they were found guilty of violating department protocols, even for off-duty infractions that were not crimes.

“The purpose of the bill was to correct an inequity that existed between the NYPD and the rest of law enforcement in New York state,” said Michael Palladino, president of the detectives union. “That inequity was exploited and abused by management so often, it gave us plenty of ammunition to convince the Legislature and the governor they needed to act.”

Most of the eight police officers have 25 to 30 years of service and were so eager to preserve their pensions, they surrendered years of “terminal leave” — worth hundreds of thousands of dollars that could have boosted their pensions still higher — rather than risk being convicted of a felony while on the department payroll.

The law was sparked by two infamous cases.

One involved a Bronx detective who lost his pension after he was convicted of perjury for denying he grilled a teen suspect — who had recorded the interrogation.

The other involved a lieutenant who shot and killed himself after he ordered a sergeant to tase an emotionally disturbed man who was menacing cops from a second-story ledge. The man fell to his death, and the officer committed suicide because he feared losing his pension.