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
- Remove the check digit (the last digit):
123456782
- Extract the even digits (2,4,6,8th digit):
12345678
- Double them:
2 4 6 8
——–>4 8 12 16
- Add the digits together:
4+8+1+2+1+6 = 22
- Add the Odd placed digits:
1+3+5+7 = 16
—>Total : 38
Validity Algorithm
- If the total is a multiple of 10, the check digit should be zero.
- Otherwise, Subtract the Total from the next highest multiple of 10 (40 in this case)
- 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,