' This is sha160 and sha256 implemented in pure QB64 with no declare library.
' This is not intended to be efficient. It is very slow.
' It is meant more as a fun and perhaps informative demonstration than as a
' serious library.
' It is a loose translation of:
' http://www.network54.com/Forum/613583/message/1351232518/
' however, without the wildcard capability, and without the file times.
'* Precalculate the powers of 2 rather than calculating every time. Exponentiation = CPU cycle hog.
PowerOfTwo(0) = 1
PowerOfTwo(p) = PowerOfTwo(p - 1) * 2
cmd$ = "256 SHA256MCalkins.bas"
resethash
remaining = filesize
remaining = remaining - 64
nextchunk
finalchunk filesize
PRINT hexq
(filesize
);
" "; gethex;
" ";
MID$(cmd$
, x
+ 1)
PRINT "Expirimental file hasher (pure qb64 version). Revision 2012 10 26" PRINT "Public domain, Michael Calkins." PRINT "160 and 256 bit hashes derived from fips180-3_final.pdf" PRINT "", "sha {160 | 256} filename"
K(0) = &H428A2F98: K(1) = &H71374491: K(2) = &HB5C0FBCF: K(3) = &HE9B5DBA5: K(4) = &H3956C25B: K(5) = &H59F111F1: K(6) = &H923F82A4: K(7) = &HAB1C5ED5
K(8) = &HD807AA98: K(9) = &H12835B01: K(10) = &H243185BE: K(11) = &H550C7DC3: K(12) = &H72BE5D74: K(13) = &H80DEB1FE: K(14) = &H9BDC06A7: K(15) = &HC19BF174
K(16) = &HE49B69C1: K(17) = &HEFBE4786: K(18) = &H0FC19DC6: K(19) = &H240CA1CC: K(20) = &H2DE92C6F: K(21) = &H4A7484AA: K(22) = &H5CB0A9DC: K(23) = &H76F988DA
K(24) = &H983E5152: K(25) = &HA831C66D: K(26) = &HB00327C8: K(27) = &HBF597FC7: K(28) = &HC6E00BF3: K(29) = &HD5A79147: K(30) = &H06CA6351: K(31) = &H14292967
K(32) = &H27B70A85: K(33) = &H2E1B2138: K(34) = &H4D2C6DFC: K(35) = &H53380D13: K(36) = &H650A7354: K(37) = &H766A0ABB: K(38) = &H81C2C92E: K(39) = &H92722C85
K(40) = &HA2BFE8A1: K(41) = &HA81A664B: K(42) = &HC24B8B70: K(43) = &HC76C51A3: K(44) = &HD192E819: K(45) = &HD6990624: K(46) = &HF40E3585: K(47) = &H106AA070
K(48) = &H19A4C116: K(49) = &H1E376C08: K(50) = &H2748774C: K(51) = &H34B0BCB5: K(52) = &H391C0CB3: K(53) = &H4ED8AA4A: K(54) = &H5B9CCA4F: K(55) = &H682E6FF3
K(56) = &H748F82EE: K(57) = &H78A5636F: K(58) = &H84C87814: K(59) = &H8CC70208: K(60) = &H90BEFFFA: K(61) = &HA4506CEB: K(62) = &HBEF9A3F7: K(63) = &HC67178F2
mode = m
shl = x * PowerOfTwo(n)
shr = x \ PowerOfTwo(n)
shlq = x * PowerOfTwo(n)
shrq = x \ PowerOfTwo(n)
rol
= shl
(x
, n
) OR shr
(x
, 32 - n
)
ror
= shr
(x
, n
) OR shl
(x
, 32 - n
)
sum0
= ror
(x
, 2) XOR ror
(x
, 13) XOR ror
(x
, 22)
sum1
= ror
(x
, 6) XOR ror
(x
, 11) XOR ror
(x
, 25)
sel0
= ror
(x
, 7) XOR ror
(x
, 18) XOR shr
(x
, 3)
sel1
= ror
(x
, 17) XOR ror
(x
, 19) XOR shr
(x
, 10)
H(0) = &H67452301: H(1) = &HEFCDAB89: H(2) = &H98BADCFE: H(3) = &H10325476: H(4) = &HC3D2E1F0
H(0) = &H6A09E667: H(1) = &HBB67AE85: H(2) = &H3C6EF372: H(3) = &HA54FF53A: H(4) = &H510E527F: H(5) = &H9B05688C: H(6) = &H1F83D9AB: H(7) = &H5BE0CD19
W
(t \
4) = shl
(W
(t \
4), 8) OR ASC(M
, 1 + t
)
W
(t \
4) = shl
(W
(t \
4), 8) OR ASC(M
, 1 + t
)W
(t \
4) = shl
(W
(t \
4), 8) OR &H80t = t + 1
W(t \ 4) = shl(W(t \ 4), 8)
t = t + 1
t = 0
t = shr(t, 2)
W(t) = 0
t = t + 1
W(14) = shrq(l, 29)
W(15) = shlq(l, 3)
W
(t
) = rol
(W
(t
- 3) XOR W
(t
- 8) XOR W
(t
- 14) XOR W
(t
- 16), 1)
a = H(0): b = H(1): c = H(2): d = H(3): e = H(4)
Te = rol(a, 5) + Ch(b, c, d) + e + &H5A827999 + W(t)
e = d: d = c: c = rol(b, 30): b = a: a = Te
Te = rol(a, 5) + Parity(b, c, d) + e + &H6ED9EBA1 + W(t)
e = d: d = c: c = rol(b, 30): b = a: a = Te
Te = rol(a, 5) + Maj(b, c, d) + e + &H8F1BBCDC + W(t)
e = d: d = c: c = rol(b, 30): b = a: a = Te
Te = rol(a, 5) + Parity(b, c, d) + e + &HCA62C1D6 + W(t)
e = d: d = c: c = rol(b, 30): b = a: a = Te
H(0) = H(0) + a: H(1) = H(1) + b: H(2) = H(2) + c: H(3) = H(3) + d: H(4) = H(4) + e
W(t) = sel1(W(t - 2)) + W(t - 7) + sel0(W(t - 15)) + W(t - 16)
a = H(0): b = H(1): c = H(2): d = H(3): e = H(4): f = H(5): g = H(6): hh = H(7)
T1 = hh + sum1(e) + Ch(e, f, g) + K(t) + W(t)
T2 = sum0(a) + Maj(a, b, c)
hh = g: g = f: f = e: e = d + T1: d = c: c = b: b = a: a = T1 + T2
H(0) = H(0) + a: H(1) = H(1) + b: H(2) = H(2) + c: H(3) = H(3) + d: H(4) = H(4) + e: H(5) = H(5) + f: H(6) = H(6) + g: H(7) = H(7) + hh
t = ""
FOR i
= 0 TO mode \
32 - 1 t = t + hexd(H(i))
gethex = t