Ab initio calculation of magnetic shielding with a finite perturbation method