Check The Validity/Checksum of The Canadian SIN in Apex

In general, following is the method to check the validity and Checksum of Canadian SIN.

Given an example Number: 123 456 782

  1. Remove the check digit (the last digit): 123456782
  2. Extract the even digits (2,4,6,8th digit): 12345678
  3. Double them: 2 4 6 8 ——–> 4 8 12 16
  4. Add the digits together:4+8+1+2+1+6 = 22
  5. Add the Odd placed digits: 1+3+5+7 = 16 —> Total : 38

Validity Algorithm

  1. If the total is a multiple of 10, the check digit should be zero.
  2. Otherwise, Subtract the Total from the next highest multiple of 10 (40 in this case)
  3. The check digit for this SIN must be equal to the difference of the number and the totals from earlier (in this case, 40-38 = 2; check digit is 2, so the number is valid)

In Apex, this logic will be like,

Source: https://stackoverflow.com/questions/4263398/how-do-i-check-the-validity-of-the-canadian-social-insurance-number-in-c

Leave a Comment