JOHANNESBURG — Brushing aside protests by press-freedom advocates and heroes of South Africa’s anti-apartheid struggle, Parliament overwhelmingly passed a contentious bill on Tuesday that will severely restrict the ability of journalists to report any information deemed to be a government secret.

The legislation, which still must undergo further steps to become law, would make it a crime, punishable by lengthy prison terms, to disseminate anything that any state agency regards as classified. Critics have called the legislation a throwback to the apartheid regime’s harsh repression and say it is meant to protect corrupt officials from press scrutiny.

Anger over the legislation was embodied by the presentation of an article published last week in The Mail & Guardian, a major weekly newspaper here, about Mac Maharaj, the spokesman for President Jacob Zuma. Most of the text had been blacked out. This outcome, the paper’s editor said, was what loomed for South Africa’s press if the legislation became law.

The Protection of Information Bill, as the legislation is called, must still clear a national council of provinces before it takes effect. Critics have said they will challenge it in South Africa’s constitutional court.