Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Ver36 = function() {
- "use strict";
- var N = 18, N2 = N*2;
- var d18_op; // D18 group operation table
- var d18_inv; // D18 group inverse
- var perm; // the permutation decomposed into cycles
- var a2i,i2a; // ASCII translation tables
- var NA=0xff; // invalid digit marker in ASCII table
- // initialization code
- (function() {
- var A8 = typeof Uint8Array != 'undefined' ? Uint8Array : Array;
- // prepare Cayley table for D18 group
- d18_op = new A8(N2*N2);
- var i, j;
- for (i = 0; i < N; i++)
- for (j = 0; j < N; j++)
- d18_op[N2*i+j] = (i + j) % N;
- for (i = 0; i < N; i++)
- for (j = N; j < N2; j++)
- d18_op[N2*i+j] = N + (i + j) % N;
- for (i = N; i < N2; i++)
- for (j = 0; j < N; j++)
- d18_op[N2*i+j] = N + (i - j) % N;
- for (i = N; i < N2; i++)
- for (j = N; j < N2; j++)
- d18_op[N2*i+j] = (N + i - j) % N;
- // prepare inverse element table
- d18_inv = new A8(N2);
- d18_inv[0] = 0;
- for (i = 1; i < N; i++)
- d18_inv[i] = N - i;
- for (i = N; i < N2; i++)
- d18_inv[i] = i;
- // decompose permutation cycles
- perm = new Array(N2);
- var p = [ // the permutation
- 29,0,32,11,35,20,7,27,2,4,19,28,30,1,5,12,3,9,16,
- 22,6,33,8,24,26,21,14,10,34,31,15,25,17,13,23,18
- ], b = new A8(N2); // assert p.length == N2
- i = 0;
- while (i < b.length) {
- var h = [null], c = h, l = 0;
- while (!b[i]) {
- var cc = [null, p[i]];
- c[0] = cc; c = cc;
- b[i] = true;
- i = p[i];
- l++;
- }
- c[0] = h[0];
- for (var j = 0; j < l; j++) {
- var pp = perm[c[1]] = new A8(l);
- for (var k = 0; k < l; k++) {
- pp[k] = c[1];
- c = c[0];
- }
- c = c[0];
- }
- for (i = 0; i < b.length && b[i]; i++) {}
- }
- // set up translation tables
- a2i = new A8(256);
- for (i = 0; i < a2i.length; i++) a2i[i] = NA;
- for (i = 0x30; i < 0x3a; i++) a2i[i] = i - 0x30;
- for (i = 0x41; i < 0x5b; i++) a2i[i] = i - 0x41 + 10;
- for (i = 0x61; i < 0x7b; i++) a2i[i] = i - 0x61 + 10;
- i2a = new A8(N2);
- for (i = 0; i < 10; i++) i2a[i] = i + 0x30;
- for (i = 10; i < N2; i++) i2a[i] = i + 0x41 - 10;
- })();
- // group operation
- var op = function(i, j) {
- return d18_op[N2*i+j];
- }
- // compute n-th application of the permutation
- var nperm = function(i, n) {
- return perm[i][n % perm[i].length];
- }
- // verify correctness of check digit
- var verify = function(str) {
- var i = 0, c = 0, l = str.length;
- while (l--) {
- var ch = a2i[str.charCodeAt(l) & 0xff];
- if (ch != NA)
- c = op(c,nperm(ch,i++));
- }
- return c;
- }
- // Create new check digit
- var create = function(str) {
- var i = 1, c = 0, l = str.length;
- while (l--) {
- var ch = a2i[str.charCodeAt(l) & 0xff];
- if (ch != NA)
- c = op(c,nperm(ch,i++));
- }
- return i2a[d18_inv[c]];
- }
- // Append check digit to given string
- var append = function(str) {
- return str + String.fromCharCode(create(str));
- }
- // export functions
- return {
- 'verify': verify,
- 'create': create,
- 'append': append
- };
- }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement