Created
November 21, 2018 07:33
-
-
Save aaronyoo/91ec6bba2fb47a2b77aab471efd86498 to your computer and use it in GitHub Desktop.
An implementation of CictroHash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pad(txt): | |
while (len(txt) % 4 != 0): | |
txt += '\0' | |
return txt | |
def create_w(s): | |
w = [[s[0], s[1], s[2], s[3]], | |
[s[4], s[5], s[6], s[7]]] | |
return w | |
def f(S): | |
w = create_w(S) | |
for _ in range(50): | |
triangle(w) | |
return [w[0][0], w[0][1], w[0][2], w[0][3], w[1][0], w[1][1], w[1][2], w[1][3]] | |
def triangle(w): | |
a(w) | |
b(w) | |
c(w) | |
d(w) | |
def a(w): | |
w[0], w[1] = w[1], w[0] | |
def b(w): | |
w[0][0] ^= w[1][3] | |
w[0][1] ^= w[1][2] | |
w[0][2] ^= w[1][1] | |
w[0][3] ^= w[1][0] | |
def c(w): | |
t = [[w[1][3], w[1][0], w[1][2], w[0][0]], | |
[w[1][1], w[0][3], w[0][1], w[0][2]]] | |
w[0][0] = t[0][0] | |
w[0][1] = t[0][1] | |
w[0][2] = t[0][2] | |
w[0][3] = t[0][3] | |
w[1][0] = t[1][0] | |
w[1][1] = t[1][1] | |
w[1][2] = t[1][2] | |
w[1][3] = t[1][3] | |
def d(w): | |
def rotate_right(n): | |
return ((n >> 1) | (n << 7)) & 0b11111111 | |
def rotate_left(n): | |
return ((n << 1) | (n >> 7)) & 0b11111111 | |
w[0][0] = rotate_left(w[0][0]) | |
w[1][0] = rotate_left(w[1][0]) | |
w[0][2] = rotate_left(w[0][2]) | |
w[1][2] = rotate_left(w[1][2]) | |
w[0][1] = rotate_right(w[0][1]) | |
w[1][1] = rotate_right(w[1][1]) | |
w[0][3] = rotate_right(w[0][3]) | |
w[1][3] = rotate_right(w[1][3]) | |
def hash(pre_image): | |
S = [31, 56, 156, 167, 38, 240, 174, 248] | |
pre = pre_image | |
pre = pad(pre) | |
for i in range(0, len(pre), 4): | |
pre_block = pre[i:i+4] | |
for j in range(4): | |
S[j] ^= ord(pre_block[j]) | |
S = f(S) | |
answer = "" | |
for i in range(4): | |
answer += "{0:x}".format(S[i]) | |
return answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment