-
Notifications
You must be signed in to change notification settings - Fork 1
/
cipher.awk
57 lines (46 loc) · 954 Bytes
/
cipher.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/awk -f
function ord_init( _low, _high, _i, _t )
{
_low = 0;
_high = 127;
for( _i = _low; _i <= _high; _i++ ) {
_t = sprintf( "%c", _i );
ord_data[ _t ] = _i;
}
}
function ord( _s, _i )
{
return ord_data[ substr( _s, _i, 1 ) ]
}
function chr( _c )
{
return sprintf( "%c", _c );
}
function getToken( idx, _alphabet, _str, _base )
{
_alphabet = "7pdy3jbhvms5zxrftnc9gqw";
_base = length( _alphabet );
idx += 4294836226;
while( idx > 0 ) {
_str = _str substr( _alphabet, idx % _base + 1, 1 );
idx = int( idx / _base );
}
return _str;
}
function getChecksum( input, _a, _b, _hash, _idx )
{
_a = 378551;
_b = 63689;
_hash = 0;
_i = 0;
for( _i = 0; _i < length( input ); _i++ ) {
_hash = ( _hash * _a + ord( input, _i + 1 ) ) % 2147483648;
_a = ( _a * _b ) % 65536;
}
return 4294967295 - _hash;
}
BEGIN {
ord_init( );
x = "sorcerykid";
print getChecksum( x ) "->" getToken( getChecksum( x ) )
}