comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » IDL implementation of SHA1 checksum
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
IDL implementation of SHA1 checksum [message #85834] Thu, 12 September 2013 12:23 Go to next message
John Correira is currently offline  John Correira
Messages: 25
Registered: August 2011
Junior Member
All:

I wanted to be able to calculate SHA1 hashes from within IDL (without
resorting to SPAWN). Since I've never seen another IDL implementation I
thought I'd share what I cooked up with the group. It works with either
a file or a string as input.

IDL> print, jc_sha1('The quick brown fox jumps over the lazy dog')
2fd4e1c67a2d28fced849ee1bb76e7391b93eb12

IDL> print, jc_sha1('')
da39a3ee5e6b4b0d3255bfef95601890afd80709

IDL> print, jc_sha1('/path/to/an/empty.file')
da39a3ee5e6b4b0d3255bfef95601890afd80709

I have not tested this on Windows or Mac (or a big endian machine).

Best regards,

John


------------------------------

function jc_SHAfunction1, x, y, z
return, (x AND y) OR ((NOT x) AND z)
end

function jc_SHAfunction2, x, y, z
return, x XOR y XOR z
end

function jc_SHAfunction3, x, y, z
return, (x AND y) OR (x AND z) OR (y AND z)
end

;;;;;;;;;;;;;;;;;;;


function jc_sha1, input, STRING=STRING, FILE=FILE

COMPILE_OPT IDL2, STRICTARRSUBS

isFile = file_test(input) or KEYWORD_SET(FILE)
if KEYWORD_SET(STRING) then isFile=0
if isFile then begin
isZeroLength = file_test(input,/ZERO_LENGTH)
canRead = file_test(input,/READ)
if ~(canRead) then begin
print, "Can't read this file"
return, -1
endif
bytearr = isZeroLength ? byte('') : read_binary(input)
endif else begin
bytearr = byte(input)
endelse

mlen = bytearr[0] eq 0b ? 0ULL : 8ULL*N_ELEMENTS(bytearr)
bytearr = bytearr[0] eq 0 ? 128b : [TEMPORARY(bytearr),128b]
while (8*N_ELEMENTS(bytearr) mod 512) ne 448 do $
bytearr = [TEMPORARY(bytearr),0b]
bytearr = [TEMPORARY(bytearr),reverse(byte(mlen,0,8))]
message = ulong(bytearr)

h0 = '67452301'xul
h1 = 'EFCDAB89'xul
h2 = '98BADCFE'xul
h3 = '10325476'xul
h4 = 'C3D2E1F0'xul

w0 = ULONARR(80)

for chind=0, n_elements(message)-1, 64 do begin

M = message[chind:chind+63]
w = w0
for i=0,15 do $
w[i] = TOTAL(M[i*4:i*4+3]*[16777216ul,65536ul,256ul,1ul],$
/PRESERVE_TYPE)
temp = w
for i=16,79 do begin
temp = w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]
w[i] = (temp*2ul) OR (temp/2147483648ul)
endfor

a = h0
b = h1
c = h2
d = h3
e = h4

for i=0, 19 do begin
temp = ((a*32ul) OR (a/134217728ul)) + jc_SHAfunction1(b,c,d) $
+ e + 1518500249ull + w[i]
e = d
d = c
c = (b*1073741824ul) OR (b/4ul)
b = a
a = ulong(temp)
endfor
for i=20, 39 do begin
temp = ((a*32ul) OR (a/134217728ul)) + jc_SHAfunction2(b,c,d) $
+ e + 1859775393ull + w[i]
e = d
d = c
c = (b*1073741824ul) OR (b/4ul)
b = a
a = ulong(temp)
endfor
for i=40, 59 do begin
temp = ((a*32ul) OR (a/134217728ul)) + jc_SHAfunction3(b,c,d) $
+ e + 2400959708ull + w[i]
e = d
d = c
c = (b*1073741824ul) OR (b/4)
b = a
a = ulong(temp)
endfor
for i=60, 79 do begin
temp = ((a*32ul) OR (a/134217728ul)) + jc_SHAfunction2(b,c,d) $
+ e + 3395469782ull + w[i]
e = d
d = c
c = (b*1073741824ul) OR (b/4ul)
b = a
a = ulong(temp)
endfor

h0 += a
h1 += b
h2 += c
h3 += d
h4 += e

endfor

h0 = string(h0,format='(z08)')
h1 = string(h1,format='(z08)')
h2 = string(h2,format='(z08)')
h3 = string(h3,format='(z08)')
h4 = string(h4,format='(z08)')

return, h0+h1+h2+h3+h4
end
Re: IDL implementation of SHA1 checksum [message #85857 is a reply to message #85834] Fri, 13 September 2013 22:45 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Thursday, September 12, 2013 3:23:35 PM UTC-4, John Correira wrote:
> All:
>
>
>
> I wanted to be able to calculate SHA1 hashes from within IDL (without
> resorting to SPAWN). Since I've never seen another IDL implementation I
> thought I'd share what I cooked up with the group. It works with either
> a file or a string as input.

That's pretty nifty!
Craig
Re: IDL implementation of SHA1 checksum [message #85859 is a reply to message #85834] Sat, 14 September 2013 04:20 Go to previous message
Haje Korth is currently offline  Haje Korth
Messages: 651
Registered: May 1997
Senior Member
Thanks for sharing!


On Thursday, September 12, 2013 3:23:35 PM UTC-4, John Correira wrote:
> All:
>
>
>
> I wanted to be able to calculate SHA1 hashes from within IDL (without
>
> resorting to SPAWN). Since I've never seen another IDL implementation I
>
> thought I'd share what I cooked up with the group. It works with either
>
> a file or a string as input.
>
>
>
> IDL> print, jc_sha1('The quick brown fox jumps over the lazy dog')
>
> 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
>
>
>
> IDL> print, jc_sha1('')
>
> da39a3ee5e6b4b0d3255bfef95601890afd80709
>
>
>
> IDL> print, jc_sha1('/path/to/an/empty.file')
>
> da39a3ee5e6b4b0d3255bfef95601890afd80709
>
>
>
> I have not tested this on Windows or Mac (or a big endian machine).
>
>
>
> Best regards,
>
>
>
> John
>
>
>
>
>
> ------------------------------
>
>
>
> function jc_SHAfunction1, x, y, z
>
> return, (x AND y) OR ((NOT x) AND z)
>
> end
>
>
>
> function jc_SHAfunction2, x, y, z
>
> return, x XOR y XOR z
>
> end
>
>
>
> function jc_SHAfunction3, x, y, z
>
> return, (x AND y) OR (x AND z) OR (y AND z)
>
> end
>
>
>
> ;;;;;;;;;;;;;;;;;;;
>
>
>
>
>
> function jc_sha1, input, STRING=STRING, FILE=FILE
>
>
>
> COMPILE_OPT IDL2, STRICTARRSUBS
>
>
>
> isFile = file_test(input) or KEYWORD_SET(FILE)
>
> if KEYWORD_SET(STRING) then isFile=0
>
> if isFile then begin
>
> isZeroLength = file_test(input,/ZERO_LENGTH)
>
> canRead = file_test(input,/READ)
>
> if ~(canRead) then begin
>
> print, "Can't read this file"
>
> return, -1
>
> endif
>
> bytearr = isZeroLength ? byte('') : read_binary(input)
>
> endif else begin
>
> bytearr = byte(input)
>
> endelse
>
>
>
> mlen = bytearr[0] eq 0b ? 0ULL : 8ULL*N_ELEMENTS(bytearr)
>
> bytearr = bytearr[0] eq 0 ? 128b : [TEMPORARY(bytearr),128b]
>
> while (8*N_ELEMENTS(bytearr) mod 512) ne 448 do $
>
> bytearr = [TEMPORARY(bytearr),0b]
>
> bytearr = [TEMPORARY(bytearr),reverse(byte(mlen,0,8))]
>
> message = ulong(bytearr)
>
>
>
> h0 = '67452301'xul
>
> h1 = 'EFCDAB89'xul
>
> h2 = '98BADCFE'xul
>
> h3 = '10325476'xul
>
> h4 = 'C3D2E1F0'xul
>
>
>
> w0 = ULONARR(80)
>
>
>
> for chind=0, n_elements(message)-1, 64 do begin
>
>
>
> M = message[chind:chind+63]
>
> w = w0
>
> for i=0,15 do $
>
> w[i] = TOTAL(M[i*4:i*4+3]*[16777216ul,65536ul,256ul,1ul],$
>
> /PRESERVE_TYPE)
>
> temp = w
>
> for i=16,79 do begin
>
> temp = w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]
>
> w[i] = (temp*2ul) OR (temp/2147483648ul)
>
> endfor
>
>
>
> a = h0
>
> b = h1
>
> c = h2
>
> d = h3
>
> e = h4
>
>
>
> for i=0, 19 do begin
>
> temp = ((a*32ul) OR (a/134217728ul)) + jc_SHAfunction1(b,c,d) $
>
> + e + 1518500249ull + w[i]
>
> e = d
>
> d = c
>
> c = (b*1073741824ul) OR (b/4ul)
>
> b = a
>
> a = ulong(temp)
>
> endfor
>
> for i=20, 39 do begin
>
> temp = ((a*32ul) OR (a/134217728ul)) + jc_SHAfunction2(b,c,d) $
>
> + e + 1859775393ull + w[i]
>
> e = d
>
> d = c
>
> c = (b*1073741824ul) OR (b/4ul)
>
> b = a
>
> a = ulong(temp)
>
> endfor
>
> for i=40, 59 do begin
>
> temp = ((a*32ul) OR (a/134217728ul)) + jc_SHAfunction3(b,c,d) $
>
> + e + 2400959708ull + w[i]
>
> e = d
>
> d = c
>
> c = (b*1073741824ul) OR (b/4)
>
> b = a
>
> a = ulong(temp)
>
> endfor
>
> for i=60, 79 do begin
>
> temp = ((a*32ul) OR (a/134217728ul)) + jc_SHAfunction2(b,c,d) $
>
> + e + 3395469782ull + w[i]
>
> e = d
>
> d = c
>
> c = (b*1073741824ul) OR (b/4ul)
>
> b = a
>
> a = ulong(temp)
>
> endfor
>
>
>
> h0 += a
>
> h1 += b
>
> h2 += c
>
> h3 += d
>
> h4 += e
>
>
>
> endfor
>
>
>
> h0 = string(h0,format='(z08)')
>
> h1 = string(h1,format='(z08)')
>
> h2 = string(h2,format='(z08)')
>
> h3 = string(h3,format='(z08)')
>
> h4 = string(h4,format='(z08)')
>
>
>
> return, h0+h1+h2+h3+h4
>
> end
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Question about CURVEFIT function
Next Topic: Widget programing - Please help!

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:08:00 PDT 2025

Total time taken to generate the page: 0.00428 seconds