add sha1 function
This commit is contained in:
parent
7bfe9c246a
commit
1657b39fb7
81
shamwow.rb
81
shamwow.rb
@ -6,7 +6,11 @@ end
|
||||
|
||||
# treat all numbers as if they are 32-bit integers
|
||||
def ror(num, shift)
|
||||
((num >> shift) | (num << (32-shift)) & ((2 ** 32) - 1))
|
||||
(((num >> shift) | (num << (32-shift))) & ((2 ** 32) - 1))
|
||||
end
|
||||
|
||||
def lor(num, shift)
|
||||
(((num << shift) | (num >> (32-shift))) & ((2 ** 32) - 1))
|
||||
end
|
||||
|
||||
def sha2(message)
|
||||
@ -98,3 +102,78 @@ def sha2(message)
|
||||
|
||||
("%08x" % h0).concat("%08x" % h1).concat("%08x" % h2).concat("%08x" % h3).concat("%08x" % h4).concat("%08x" % h5).concat("%08x" % h6).concat("%08x" % h7)
|
||||
end
|
||||
|
||||
def sha1(message)
|
||||
|
||||
message_in_bits = message.unpack("B*")[0]
|
||||
|
||||
h0 = 0x67452301
|
||||
h1 = 0xEFCDAB89
|
||||
h2 = 0x98BADCFE
|
||||
h3 = 0x10325476
|
||||
h4 = 0xC3D2E1F0
|
||||
|
||||
len = message_in_bits.length
|
||||
|
||||
bits = message_in_bits
|
||||
bits << "1"
|
||||
bits << "0" * (512 - ((bits.length + 64) % 512))
|
||||
bits << "%064b" % len
|
||||
|
||||
chunked = chunker(bits, 512)
|
||||
|
||||
i = 0
|
||||
while i < chunked.length
|
||||
m = []
|
||||
|
||||
message = chunker(chunked[i], 32)
|
||||
|
||||
message.each_with_index { |word, index| m[index] = word.to_i(2) }
|
||||
|
||||
(16..79).each { |word|
|
||||
m[word] = lor((m[word-3] ^ m[word-8] ^ m[word-14] ^ m[word-16]), 1) & 0xFFFFFFFF
|
||||
}
|
||||
|
||||
a = h0
|
||||
b = h1
|
||||
c = h2
|
||||
d = h3
|
||||
e = h4
|
||||
|
||||
(0..79).each { |word|
|
||||
if (0..19).include? word
|
||||
f = ((b & c) | (~b & d))
|
||||
k = 0x5A827999
|
||||
end
|
||||
if (20..39).include? word
|
||||
f = (b ^ c ^ d)
|
||||
k = 0x6ED9EBA1
|
||||
end
|
||||
if (40..59).include? word
|
||||
f = (b & c) | (b & d) | (c & d)
|
||||
k = 0x8F1BBCDC
|
||||
end
|
||||
if (60..79).include? word
|
||||
f = (b ^ c ^ d)
|
||||
k = 0xCA62C1D6
|
||||
end
|
||||
|
||||
temp = (lor(a, 5) + f + e + k + m[word]) & 0xFFFFFFFF
|
||||
e = d
|
||||
d = c
|
||||
c = lor(b, 30)
|
||||
b = a
|
||||
a = temp
|
||||
}
|
||||
|
||||
h0 = (h0 + a) & 0xFFFFFFFF
|
||||
h1 = (h1 + b) & 0xFFFFFFFF
|
||||
h2 = (h2 + c) & 0xFFFFFFFF
|
||||
h3 = (h3 + d) & 0xFFFFFFFF
|
||||
h4 = (h4 + e) & 0xFFFFFFFF
|
||||
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
("%08x" % h0).concat("%08x" % h1).concat("%08x" % h2).concat("%08x" % h3).concat("%08x" % h4)
|
||||
end
|
@ -3,9 +3,12 @@
|
||||
require_relative "shamwow"
|
||||
require "test/unit"
|
||||
require 'digest'
|
||||
|
||||
|
||||
class TestShamwow < Test::Unit::TestCase
|
||||
|
||||
@@preamble = "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America."
|
||||
@@gettysburg = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth."
|
||||
|
||||
def test_ror
|
||||
assert_equal(0, ror(0, 0))
|
||||
assert_equal(0x80000000, ror(1, 1))
|
||||
@ -14,10 +17,21 @@ class TestShamwow < Test::Unit::TestCase
|
||||
assert_equal("01111111111111111111111111111111".to_i(2), ror("11111111111111111111111111111110".to_i(2), 1))
|
||||
assert_equal("10111111111111110111111111111111".to_i(2), ror("11111111111111011111111111111110".to_i(2), 2))
|
||||
assert_equal("11111111111111011111111111111110".to_i(2), ror("11111111111111011111111111111110".to_i(2), 32))
|
||||
assert_equal("01111111111111111111111111111111".to_i(2), ror("11111111111111111111111111111110".to_i(2), 33))
|
||||
assert_equal("11111111111111111111111111111110".to_i(2), ror("011111111111111111111111111111101".to_i(2), 1))
|
||||
end
|
||||
|
||||
def test_lor
|
||||
assert_equal(0, lor(0, 0))
|
||||
assert_equal(2, lor(1, 1))
|
||||
assert_equal(6, lor(3, 1))
|
||||
assert_equal("00000000000000000000000000000010".to_i(2), lor("00000000000000000000000000000001".to_i(2), 1))
|
||||
assert_equal("10000000000000000000000000000010".to_i(2), lor("01000000000000000000000000000001".to_i(2), 1))
|
||||
assert_equal("00000000000000000000000000000101".to_i(2), lor("01000000000000000000000000000001".to_i(2), 2))
|
||||
assert_equal("11111111111111011111111111111110".to_i(2), lor("11111111111111011111111111111110".to_i(2), 32))
|
||||
assert_equal("11111111111111111111111111111011".to_i(2), lor("011111111111111111111111111111101".to_i(2), 1))
|
||||
assert_equal("11111111111111111111111111111011".to_i(2), lor("111111111111111111111111111111101".to_i(2), 1))
|
||||
end
|
||||
|
||||
def test_sha2
|
||||
assert_equal(Digest::SHA2.hexdigest(''), sha2(""))
|
||||
assert_equal(Digest::SHA2.hexdigest('abc'), sha2("abc"))
|
||||
@ -26,11 +40,17 @@ class TestShamwow < Test::Unit::TestCase
|
||||
assert_equal(Digest::SHA2.hexdigest("ЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя"), sha2("ЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя"))
|
||||
assert_equal(Digest::SHA2.hexdigest("𝕿𝖍𝖊 𝖖𝖚𝖎𝖈𝖐 𝖇𝖗𝖔𝖜𝖓 𝖋𝖔𝖝 𝖏𝖚𝖒𝖕𝖘 𝖔𝖛𝖊𝖗 𝖙𝖍𝖊 𝖑𝖆𝖟𝖞 𝖉𝖔𝖌"), sha2("𝕿𝖍𝖊 𝖖𝖚𝖎𝖈𝖐 𝖇𝖗𝖔𝖜𝖓 𝖋𝖔𝖝 𝖏𝖚𝖒𝖕𝖘 𝖔𝖛𝖊𝖗 𝖙𝖍𝖊 𝖑𝖆𝖟𝖞 𝖉𝖔𝖌"))
|
||||
|
||||
preamble = "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America."
|
||||
assert_equal(Digest::SHA2.hexdigest(preamble), sha2(preamble))
|
||||
assert_equal(Digest::SHA2.hexdigest(@@preamble), sha2(@@preamble))
|
||||
assert_equal(Digest::SHA2.hexdigest(@@gettysburg), sha2(@@gettysburg))
|
||||
end
|
||||
|
||||
gettysburg = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth."
|
||||
assert_equal(Digest::SHA2.hexdigest(gettysburg), sha2(gettysburg))
|
||||
def test_sha1
|
||||
assert_equal(Digest::SHA1.hexdigest(''), sha1(''))
|
||||
assert_equal(Digest::SHA1.hexdigest('abc'), sha1('abc'))
|
||||
assert_equal(Digest::SHA1.hexdigest("𝕿𝖍𝖊 𝖖𝖚𝖎𝖈𝖐 𝖇𝖗𝖔𝖜𝖓 𝖋𝖔𝖝 𝖏𝖚𝖒𝖕𝖘 𝖔𝖛𝖊𝖗 𝖙𝖍𝖊 𝖑𝖆𝖟𝖞 𝖉𝖔𝖌"), sha1("𝕿𝖍𝖊 𝖖𝖚𝖎𝖈𝖐 𝖇𝖗𝖔𝖜𝖓 𝖋𝖔𝖝 𝖏𝖚𝖒𝖕𝖘 𝖔𝖛𝖊𝖗 𝖙𝖍𝖊 𝖑𝖆𝖟𝖞 𝖉𝖔𝖌"))
|
||||
|
||||
assert_equal(Digest::SHA1.hexdigest(@@preamble), sha1(@@preamble))
|
||||
assert_equal(Digest::SHA1.hexdigest(@@gettysburg), sha1(@@gettysburg))
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user