This commit is contained in:
Leslie Krause 2019-02-13 23:37:00 -05:00
commit 9e6160daff
3 changed files with 136 additions and 0 deletions

32
README.txt Normal file
View File

@ -0,0 +1,32 @@
Simple Cipher Mod v1.0
By Leslie E. Krause
Simple Cipher is a lightweight, portable hashing algorithm providing a minimal degree
of security for non mission-critical data (e.g. unique, non-guessable URLs).
Source Code License
----------------------
MIT License
Copyright (c) 2016-2019, Leslie E. Krause.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
For more details:
https://opensource.org/licenses/MIT

57
cipher.awk Normal file
View File

@ -0,0 +1,57 @@
#!/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 ) )
}

47
init.lua Normal file
View File

@ -0,0 +1,47 @@
--------------------------------------------------------
-- Minetest :: Simple Cipher Mod v1.0 (cipher)
--
-- See README.txt for licensing and other information.
-- Copyright (c) 2016-2019, Leslie E. Krause
--
-- ./games/just_test_tribute/mods/cipher/init.lua
--------------------------------------------------------
-- alphabet soup to be used by the tokenizer
local alphabet = "pdy3jbh7vms5zxrftnc9gqw"
cipher = { }
cipher.tokenize = function ( hash )
local base = #alphabet
local str = ""
hash = hash + 4294836226
while hash > 0 do
local idx = hash % base + 1
str = str .. string.sub( alphabet, idx, idx )
hash = math.floor( hash / base )
end
return str
end
cipher.get_checksum = function ( input )
local a = 378551
local b = 63689
local hash = 0
local i = 0
for i = 1, #input do
hash = ( hash * a + string.byte( input, i ) ) % 2147483648
a = ( a * b ) % 65536
end
return 4294967295 - hash
end
if cipher.tokenize( cipher.get_checksum( "sorcerykid" ) ) ~= "gfwd9pmd" then
-- basic sanity check upon startup
error( "[cipher] Failed to generate correct token from hash!" )
end