WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sample code
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Digital FingerPrinting using SHA1


SHA-1 (Secure Hashing Algorithm 1) digital fingerprint of a string.

Reference:
http://www.w3.org/PICS/DSig/SHA1_1_0.html

http://www.faqs.org/rfcs/rfc3174.html
hex_chr = "0123456789abcdef"

to_SHA1 = "Fingerprint this ..."
Gosub Do_SHA1
message("SHA1", from_SHA1)

Exit



:Hex

from_Hex = ""
j = 7
While j >= 0
	from_Hex = StrCat(from_Hex, StrSub(hex_chr, ((num >> (j * 4)) & 15) + 1, 1))
	j = j - 1
EndWhile

Return



:Kt

If t < 20
	from_Kt = 1518500249
Else
	If t < 40
		from_Kt = 1859775393
	Else
		If t < 60
			from_Kt = -1894007588
		Else
			from_Kt = -899497514
		EndIf
	EndIf
EndIf

Return



:Ft

If (t < 20) 
	from_Ft = (b & c) | ((~b) & d)
Else
	If (t < 40)
		from_Ft = b ^ c ^ d
	Else
		If (t < 60)
			from_Ft = (b & c) | (b & d) | (c & d)
		Else
			from_Ft = b ^ c ^ d
		EndIf
	EndIf
EndIf

Return



:Rol

from_Rol = (num << cnt) | ((num >> 32 - cnt) & (-1 ^ (-1 << cnt)))

Return



:str2blks_SHA1

lblk = StrLen(to_SHA1)
nblk = ((lblk + 8) >> 6) + 1
blksn = nblk * 16
i = 0
While i < blksn
	blks%i% = 0
	i = i + 1
EndWhile
i = 0
While i < lblk
	j = i >> 2
	blks%j% = blks%j% | Char2Num(StrSub(to_SHA1, i + 1, 1)) << (24 - (i mod 4) * 8)
	i = i + 1
EndWhile
j = lblk >> 2
blks%j% = blks%j% | 128 << (24 - (i mod 4) * 8)
j = blksn - 1
blks%j% = lblk * 8

Return



:Do_SHA1

Gosub str2blks_SHA1
Gosub init_w

a = 1732584193
b = -271733879
c = -1732584194
d = 271733878
e = -1009589776

i = 0
While i < nblk * 16
	olda = a
	oldb = b
	oldc = c
	oldd = d
	olde = e

	j = 0
	While j < 80
	   BoxText(StrCat("i=", i, @CRLF, "j=", j))

		If (j < 16)
			k = i + j
			w%j% = blks%k%
		Else
			k = j - 3
			l = j - 8
			m = j - 14
			n = j - 16
			num = w%k% ^ w%l% ^ w%m% ^ w%n%
			cnt = 1
			Gosub Rol
			w%j% = from_Rol
		EndIf

		num = a
		cnt = 5
		Gosub Rol
		f1 = from_Rol
		t = j
		Gosub Ft
		f2 = from_Ft
		f3 = f1 + f2
		f4 = e + w%j%
		t = j
		Gosub Kt
		f5 = from_Kt
		f6 = f4 + f5
		t = f3 + f6

		e = d
		d = c
		num = b
		cnt = 30
		Gosub Rol
		c = from_Rol
		b = a
		a = t

		j = j + 1
	EndWhile

	a = olda + a
	b = oldb + b
	c = oldc + c
	d = oldd + d
	e = olde + e

	i = i + 16
EndWhile

num = a
Gosub Hex
from_SHA1 = from_Hex
num = b
Gosub Hex
from_SHA1 = StrCat(from_SHA1, from_Hex)
num = c
Gosub Hex
from_SHA1 = StrCat(from_SHA1, from_Hex)
num = d
Gosub Hex
from_SHA1 = StrCat(from_SHA1, from_Hex)
num = e
Gosub Hex
from_SHA1 = StrCat(from_SHA1, from_Hex)

Return



:init_w

i = 0
While i < nblk * 16
	w%i% = 0
	i = i + 1
EndWhile

Return
Note: BinaryCheckSum can be used for MD5 (Message Digest 5 -- one way hash function ) .
Article ID:   W16176
File Created: 2004:03:30:15:43:08
Last Updated: 2004:03:30:15:43:08