ID card verification code algorithm
ID card verification code generation method:
SIGMA (AI x WI) (mod 11)
I: the serial number of the character from the start to the left, including the verification code;
AI indicates the number character value at position I;
The weighted factor at the I position of wi. Its value is Wi = Mod (Power (2, (n-1), 11)
I 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1
Check Code:
SIGMA (AI x WI) (mod 11) 0 1 2 3 4 5 6 7 8 9 10
Verification Code character value: AI 1 0x9 8 7 6 5 4 3 2
- Create or replace function id_15to18 (idcardnum varchar2) return varchar2 is
- Result varchar2 (20 );
- I smallint;
- Num smallint: = 0;
- Snum varchar2 (2 );
- Code char (1 );
- Temp17 varchar2 (20 );
- Begin
- If length (idcardnum) = 18 or idcardnum is null then
- Result: = upper (idcardnum );
- Elsif length (idcardnum) = 15 then
- Temp17: = substr (idcardnum, 1, 6) | '19' | substr (idcardnum, 7, 9 );
- I: = 18;
- While I> 1 Loop
- Num: = num + Mod (Power (2, I-1), 11) * substr (temp17, 19-I, 1 );
- I: = I-1;
- End loop;
- Num: = Mod (Num, 11 );
- Snum: = num | '';
- If num = 0 then
- Code: = '1 ';
- Elsif num = 1 then
- Code: = '0 ';
- Elsif num = 2 then
- Code: = 'X ';
- Else
- Code: = (12-num) | '';
- End if;
- Result: = temp17 | code;
- Else
- Result: = idcardnum;
- End if;
- Return result;
- Exception
- When others then
- Dbms_output.put_line ('erroe '| idcardnum );
- End id_15to18;