-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinomial_hash_function.sf
162 lines (132 loc) · 3.96 KB
/
binomial_hash_function.sf
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/ruby
# Author: Trizen
# Date: 28 October 2022
# https://github.com/trizen
# A very simple hash function (most likely, not secure), using the binomial function to create the avalanche effect.
# See also:
# https://oeis.org/A014062
# https://yewtu.be/watch?v=KqqOXndnvic
# https://en.wikipedia.org/wiki/Avalanche_effect
define (
BLOCK_SIZE = 16, # in bytes
MODULO = 2**128,
)
func process_block(str) {
func f(bits) {
bits.sum_kv {|k,v|
binomial(k*k + v, k)
} % MODULO
}
while (str.len < BLOCK_SIZE) {
var arr = str.bytes.map{.inc}
str += arr.sum
str += arr.prod
}
var A = f(str.ascii2bits)
var B = f(str.reverse.ascii2bits)
mulmod(A, B, MODULO)
}
func prepare_result(num) {
sprintf('%0*s', 2*BLOCK_SIZE, num % MODULO -> as_hex)
}
func binomial_hash_string(str) {
prepare_result(str.encode_utf8.split(BLOCK_SIZE).map(process_block).sum)
}
func binomial_hash_file(filename) {
var total = 0
var fh = File(filename).open('<:raw') || return nil
while (fh.read(\var block, BLOCK_SIZE)) {
total += process_block(block)
}
prepare_result(total)
}
var tests = [
'',
"\0",
'a',
'b',
'ab',
'ba',
'abc',
'acb',
'message digest',
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'12345678901234567890123456789012345678901234567890123456789012345678901234567890',
]
for msg in tests {
var hash = binomial_hash_string(msg)
say "#{hash} : #{msg.dump}"
}
printf("\nBinomial hash of this script: %s\n\n", binomial_hash_file(__FILE__))
#
## Run some special tests
#
assert_ne(
binomial_hash_string(BLOCK_SIZE.of(50.chr).join),
binomial_hash_string(BLOCK_SIZE.dec.of(50.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.of(16.chr).join),
binomial_hash_string(BLOCK_SIZE.dec.of(8.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.of(16.chr).join),
binomial_hash_string(BLOCK_SIZE.dec.of(16.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.dec.of(15.chr).join),
binomial_hash_string(BLOCK_SIZE.dec.dec.of(15.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.inc.of(2.chr).join),
binomial_hash_string(BLOCK_SIZE.inc.inc.of(2.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.of(15.chr).join),
binomial_hash_string(BLOCK_SIZE.dec.of(15.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.dec.of(14.chr).join),
binomial_hash_string(BLOCK_SIZE.dec.dec.of(14.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.inc.of(1.chr).join),
binomial_hash_string(BLOCK_SIZE.inc.inc.of(1.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.of(126.chr).join),
binomial_hash_string(BLOCK_SIZE.of(63.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.of(254.chr).join),
binomial_hash_string(BLOCK_SIZE.of(191.chr).join)
)
assert_ne(
binomial_hash_string(BLOCK_SIZE.of(124.chr).join),
binomial_hash_string(BLOCK_SIZE.of(62.chr).join)
)
assert_ne(
binomial_hash_string("1"),
binomial_hash_string("149"),
)
for n in (48..52) {
assert_ne(
binomial_hash_string(BLOCK_SIZE.inc.of(n.chr).join),
binomial_hash_string(BLOCK_SIZE.inc.inc.of(n.chr).join),
"Collision for n = #{n}"
)
}
__END__
00000000000000000000000000000000 : ""
fea891fdc54930fd39620fcc478299f0 : "\0"
e1a06e532bba0bcf2ef6115e06f6e410 : "a"
004cac7e4dcb9c4129aa7748278626f2 : "b"
82eb8712eb98d42f187531a7eb3f32b2 : "ab"
c8f774efd7bc079c0b749703106346c2 : "ba"
23c44e458e6e4b79109b9a0417499796 : "abc"
643c218207789c53e40d246912feb456 : "acb"
d55aee8c85258c85ed4a866bf4891014 : "message digest"
82a9f92413c1b3cac57f28488d61836b : "abcdefghijklmnopqrstuvwxyz"
c4c22251fd0388692867eefe8af04b99 : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
bfcdbe7ba1ae8243902eee55226f3a0c : "12345678901234567890123456789012345678901234567890123456789012345678901234567890"