├── README └── fngsin.class.php /README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/corbanworks/fng-sin-tools/HEAD/README -------------------------------------------------------------------------------- /fngsin.class.php: -------------------------------------------------------------------------------- 1 | 6 | 7 | FNG Canada Social Insurance Number Generator and Validator v1.1 by the Fake 8 | Name Generator is licensed to you under a Creative Commons Attribution-Share 9 | Alike 3.0 United States License. 10 | 11 | For full license details, please visit: 12 | http://www.fakenamegenerator.com/license.php 13 | 14 | */ 15 | 16 | class fngsin{ 17 | 18 | // Validates using the Luhn Algorithm (MOD10) 19 | // See: http://en.wikipedia.org/wiki/Luhn_algorithm 20 | function luhn($str){ 21 | $odd = !strlen($str)%2; 22 | $sum = 0; 23 | for($i=0;$i9?$x-9:$x; 31 | } 32 | } 33 | return(($sum%10)==0); 34 | } 35 | 36 | function validateSIN($sin){ 37 | $sin = preg_replace('/[^0-9]/','',$sin); 38 | if(strlen($sin) == 9){ 39 | if($sin[0] == '0' || $sin[0] == '8'){ 40 | return false; 41 | }else{ 42 | return $this->luhn($sin); 43 | } 44 | }else{ 45 | return false; 46 | } 47 | } 48 | 49 | function generateSIN($separator = ' '){ 50 | $validPrefix = array(1,2,3,4,5,6,7,9); 51 | $sin = array_rand($validPrefix,1); 52 | $length = 9; 53 | 54 | while(strlen($sin) < ($length - 1)){ 55 | $sin .= rand(0,9); 56 | } 57 | 58 | $sum = 0; 59 | $pos = 0; 60 | 61 | $reversedSIN = strrev( $sin ); 62 | 63 | while($pos < $length - 1){ 64 | $odd = $reversedSIN[ $pos ] * 2; 65 | if($odd > 9){ 66 | $odd -= 9; 67 | } 68 | 69 | $sum += $odd; 70 | 71 | if($pos != ($length - 2)){ 72 | $sum += $reversedSIN[ $pos +1 ]; 73 | } 74 | $pos += 2; 75 | } 76 | 77 | $checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10; 78 | $sin .= $checkdigit; 79 | 80 | $sin1 = substr($sin,0,3); 81 | $sin2 = substr($sin,3,3); 82 | $sin3 = substr($sin,6,3); 83 | 84 | return $sin1.$separator.$sin2.$separator.$sin3; 85 | } 86 | 87 | } 88 | 89 | /* Example usage: */ 90 | 91 | /* 92 | 93 | // Instantiate the class 94 | $fngsin = new fngsin(); 95 | 96 | // Generate a SIN 97 | echo $fngsin->generateSIN(); 98 | 99 | // Validate a SIN 100 | echo $fngsin->validateSIN('046 454 286'); 101 | 102 | */ 103 | ?> --------------------------------------------------------------------------------