canada-sin
v0.1.3
Published
Validate and generate Canadian Social Insurance Numbers (SIN) using the Luhn checksum algorithm.
Downloads
161
Maintainers
Readme
canada-sin
Validate and generate Canadian Social Insurance Numbers (SIN) using the Luhn checksum algorithm. Written in TypeScript, built with Vite, shipped as both ESM and CommonJS.
Generated SINs are structurally valid test data (they pass the same checksum real SINs use) — they are not real, assigned Social Insurance Numbers.
Install
npm install canada-sinUsage
import { isValidSIN, generateSIN } from "canada-sin";
isValidSIN("046 454 286"); // true
isValidSIN("123-456-789"); // false (bad checksum)
isValidSIN("12345"); // false (wrong length)
generateSIN(); // e.g. "728194625" — a fresh, structurally valid SINCommonJS:
const { isValidSIN, generateSIN } = require("canada-sin");API
isValidSIN(sin: string | number): boolean
Returns true if sin, once non-digit characters (spaces, dashes) are
stripped, is exactly 9 digits long and satisfies the Luhn checksum.
generateSIN(): string
Returns a random 9-digit string that satisfies isValidSIN. The first digit
is always 1-9 (real SINs never start with 0); the remaining digits are
randomized, and the final digit is computed as the correct Luhn check digit.
Development
npm install
npm test # run unit tests (Vitest)
npm run build # build dist/ (ESM + CJS + types) with ViteReferences
- wealthsimple/social-insurance-number — the reference implementation this package's algorithm and API were based on.
- ShirtlessKirk's Luhn algorithm gist — origin of the lookup-table Luhn checksum technique used here.
- Luhn algorithm — Wikipedia — background on the checksum algorithm itself.
License
MIT
