Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,133 @@
' version 20-10-2016
' FIPS PUB 180-4
' compile with: fbc -s console
Function SHA_256(test_str As String) As String
#Macro Ch (x, y, z)
(((x) And (y)) Xor ((Not (x)) And z))
#EndMacro
#Macro Maj (x, y, z)
(((x) And (y)) Xor ((x) And (z)) Xor ((y) And (z)))
#EndMacro
#Macro sigma0 (x)
(((x) Shr 2 Or (x) Shl 30) Xor ((x) Shr 13 Or (x) Shl 19) Xor ((x) Shr 22 Or (x) Shl 10))
#EndMacro
#Macro sigma1 (x)
(((x) Shr 6 Or (x) Shl 26) Xor ((x) Shr 11 Or (x) Shl 21) Xor ((x) Shr 25 Or (x) Shl 7))
#EndMacro
#Macro sigma2 (x)
(((x) Shr 7 Or (x) Shl 25) Xor ((x) Shr 18 Or (x) Shl 14) Xor ((x) Shr 3))
#EndMacro
#Macro sigma3 (x)
(((x) Shr 17 Or (x) Shl 15) Xor ((x) Shr 19 Or (x) Shl 13) Xor ((x) Shr 10))
#EndMacro
Dim As String message = test_str ' strings are passed as ByRef's
Dim As Long i, j
Dim As UByte Ptr ww1
Dim As UInteger<32> Ptr ww4
Dim As ULongInt l = Len(message)
' set the first bit after the message to 1
message = message + Chr(1 Shl 7)
' add one char to the length
Dim As ULong padding = 64 - ((l +1) Mod (512 \ 8)) ' 512 \ 8 = 64 char.
' check if we have enough room for inserting the length
If padding < 8 Then padding = padding + 64
message = message + String(padding, Chr(0)) ' adjust length
Dim As ULong l1 = Len(message) ' new length
l = l * 8 ' orignal length in bits
' create ubyte ptr to point to l ( = length in bits)
Dim As UByte Ptr ub_ptr = Cast(UByte Ptr, @l)
For i = 0 To 7 'copy length of message to the last 8 bytes
message[l1 -1 - i] = ub_ptr[i]
Next
'table of constants
Dim As UInteger<32> K(0 To ...) = _
{ &H428a2f98, &H71374491, &Hb5c0fbcf, &He9b5dba5, &H3956c25b, &H59f111f1, _
&H923f82a4, &Hab1c5ed5, &Hd807aa98, &H12835b01, &H243185be, &H550c7dc3, _
&H72be5d74, &H80deb1fe, &H9bdc06a7, &Hc19bf174, &He49b69c1, &Hefbe4786, _
&H0fc19dc6, &H240ca1cc, &H2de92c6f, &H4a7484aa, &H5cb0a9dc, &H76f988da, _
&H983e5152, &Ha831c66d, &Hb00327c8, &Hbf597fc7, &Hc6e00bf3, &Hd5a79147, _
&H06ca6351, &H14292967, &H27b70a85, &H2e1b2138, &H4d2c6dfc, &H53380d13, _
&H650a7354, &H766a0abb, &H81c2c92e, &H92722c85, &Ha2bfe8a1, &Ha81a664b, _
&Hc24b8b70, &Hc76c51a3, &Hd192e819, &Hd6990624, &Hf40e3585, &H106aa070, _
&H19a4c116, &H1e376c08, &H2748774c, &H34b0bcb5, &H391c0cb3, &H4ed8aa4a, _
&H5b9cca4f, &H682e6ff3, &H748f82ee, &H78a5636f, &H84c87814, &H8cc70208, _
&H90befffa, &Ha4506ceb, &Hbef9a3f7, &Hc67178f2 }
Dim As UInteger<32> h0 = &H6a09e667
Dim As UInteger<32> h1 = &Hbb67ae85
Dim As UInteger<32> h2 = &H3c6ef372
Dim As UInteger<32> h3 = &Ha54ff53a
Dim As UInteger<32> h4 = &H510e527f
Dim As UInteger<32> h5 = &H9b05688c
Dim As UInteger<32> h6 = &H1f83d9ab
Dim As UInteger<32> h7 = &H5be0cd19
Dim As UInteger<32> a, b, c, d, e, f, g, h
Dim As UInteger<32> t1, t2, w(0 To 63)
For j = 0 To (l1 -1) \ 64 ' split into block of 64 bytes
ww1 = Cast(UByte Ptr, @message[j * 64])
ww4 = Cast(UInteger<32> Ptr, @message[j * 64])
For i = 0 To 60 Step 4 'little endian -> big endian
Swap ww1[i ], ww1[i +3]
Swap ww1[i +1], ww1[i +2]
Next
For i = 0 To 15 ' copy the 16 32bit block into the array
W(i) = ww4[i]
Next
For i = 16 To 63 ' fill the rest of the array
w(i) = sigma3(W(i -2)) + W(i -7) + sigma2(W(i -15)) + W(i -16)
Next
a = h0 : b = h1 : c = h2 : d = h3 : e = h4 : f = h5 : g = h6 : h = h7
For i = 0 To 63
t1 = h + sigma1(e) + Ch(e, f, g) + K(i) + W(i)
t2 = sigma0(a) + Maj(a, b, c)
h = g : g = f : f = e
e = d + t1
d = c : c = b : b = a
a = t1 + t2
Next
h0 += a : h1 += b : h2 += c : h3 += d
h4 += e : h5 += f : h6 += g : h7 += h
Next j
Dim As String answer = Hex(h0, 8) + Hex(h1, 8) + Hex(h2, 8) + Hex(h3, 8)
answer += Hex(h4, 8) + Hex(h5, 8) + Hex(h6, 8) + Hex(h7, 8)
Return LCase(answer)
End Function
' ------=< MAIN >=------
Dim As String test = "Rosetta code"
Print test; " => "; SHA_256(test)
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,3 @@
native java.security.MessageDigest
def sha256Java( message ) = map( a -> format('%02x', a), list(MessageDigest.getInstance('SHA-256').digest(message.getBytes('UTF-8'))) ).mkString()

View file

@ -0,0 +1,83 @@
def sha256( message ) =
//Initialize hash values
h0 = 0x6a09e667
h1 = 0xbb67ae85
h2 = 0x3c6ef372
h3 = 0xa54ff53a
h4 = 0x510e527f
h5 = 0x9b05688c
h6 = 0x1f83d9ab
h7 = 0x5be0cd19
// Initialize array of round constants
k(0..63) = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
// Pre-processing
bits = BitArray( message.getBytes('UTF-8') )
len = bits.length()
bits.append( 1 )
r = bits.length()%512
bits.appendAll( 0 | _ <- 1..(if r > 448 then 512 - r + 448 else 448 - r) )
bits.appendInt( 0 )
bits.appendInt( len )
words = bits.toIntVector()
// Process the message in successive 512-bit chunks
for chunk <- 0:words.length():16
w(0..15) = words(chunk..chunk+15)
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
for i <- 16..63
s0 = (w(i-15) rotateright 7) xor (w(i-15) rotateright 18) xor (w(i-15) >>> 3)
s1 = (w(i-2) rotateright 17) xor (w(i-2) rotateright 19) xor (w(i-2) >>> 10)
w(i) = w(i-16) + s0 + w(i-7) + s1
// Initialize working variables to current hash value
a = h0
b = h1
c = h2
d = h3
e = h4
f = h5
g = h6
h = h7
// Compression function main loop
for i <- 0..63
S1 = (e rotateright 6) xor (e rotateright 11) xor (e rotateright 25)
ch = (e and f) xor ((not e) and g)
temp1 = h + S1 + ch + k(i) + w(i)
S0 = (a rotateright 2) xor (a rotateright 13) xor (a rotateright 22)
maj = (a and b) xor (a and c) xor (b and c)
temp2 = S0 + maj
h = g
g = f
f = e
e = d + temp1
d = c
c = b
b = a
a = temp1 + temp2
// Add the compressed chunk to the current hash value
h0 = h0 + a
h1 = h1 + b
h2 = h2 + c
h3 = h3 + d
h4 = h4 + e
h5 = h5 + f
h6 = h6 + g
h7 = h7 + h
// Produce the final hash value (big-endian)
map( a -> format('%08x', a.intValue()), [h0, h1, h2, h3, h4, h5, h6, h7] ).mkString()

View file

@ -0,0 +1,9 @@
message = 'Rosetta code'
println( 'FunL: "' + message + '" ~> ' + sha256(message) )
println( 'Java: "' + message + '" ~> ' + sha256Java(message) )
message = ''
println( 'FunL: "' + message + '" ~> ' + sha256(message) )
println( 'Java: "' + message + '" ~> ' + sha256Java(message) )

View file

@ -0,0 +1,10 @@
// The following will return a list of all the cipher
// algorithms supported by the installation of Lasso
cipher_list
// With a -digest parameter the method will limit the returned list
// to all of the digest algorithms supported by the installation of Lasso
cipher_list(-digest)
// return the SHA-256 digest. Dependant on SHA-256 being an available digest method
cipher_digest('Rosetta Code', -digest='SHA-256',-hex=true)

View file

@ -0,0 +1,13 @@
import strutils
const SHA256Len = 32
proc SHA256(d: cstring, n: culong, md: cstring = nil): cstring {.cdecl, dynlib: "libssl.so", importc.}
proc SHA256(s: string): string =
result = ""
let s = SHA256(s.cstring, s.len.culong)
for i in 0 .. < SHA256Len:
result.add s[i].BiggestInt.toHex(2).toLower
echo SHA256("Rosetta code")

View file

@ -0,0 +1,16 @@
constant lib = open_dll("SHA.DLL")
constant SHA_HashBlock = define_c_proc(lib,"SHA_HashBlock",{C_PTR,C_PTR,C_INT})
function sha256(string s)
atom mem = allocate(32)
sequence res
c_proc(SHA_HashBlock,{s,mem,length(s)})
res = peek4u({mem,8})
free(mem)
for i=1 to length(res) do
res[i] = sprintf("%08x",res[i])
end for
return join(res)
end function
?sha256("Rosetta code")

View file

@ -0,0 +1,126 @@
--
-- demo\rosetta\sha-256.exw
-- ========================
--
-- fairly faithful rendition of https://en.wikipedia.org/wiki/SHA-2
-- with slightly improved names (eg s0 -> sigma0) from elsewhere.
-- See also sha-256asm.exw for a faster inline asm version, and
-- sha-256dll.exw is much shorter as it uses a pre-built dll.
--Initial array of round constants
--(first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
constant k = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}
function pad64(integer v)
-- round v up to multiple of 64
return floor((v+63)/64)*64
end function
constant m4 = allocate(4) -- scratch area, for uint32
function uint32(atom v)
--
-- (note: I have experimented to call this as few times as possible.
-- It wouldn't hurt to perform this on every maths op, but a
-- few leading bits in a few work fields don't matter much.)
--
poke4(m4,v)
return peek4u(m4)
end function
function sq_uint32(sequence s)
-- apply unit32 to all elements of s
for i=1 to length(s) do
s[i] = uint32(s[i])
end for
return s
end function
function dword(string msg, integer i)
-- get dword as big-endian
return msg[i]*#1000000+msg[i+1]*#10000+msg[i+2]*#100+msg[i+3]
end function
function shr(atom v, integer bits)
return floor(v/power(2,bits))
end function
function ror(atom v, integer bits)
return or_bits(shr(v,bits),v*power(2,32-bits))
end function
function sha256(string msg)
-- main function
atom s0,s1,a,b,c,d,e,f,g,h,ch,temp1,maj,temp2,x
sequence w = repeat(0,64)
sequence res
integer len = length(msg)+1
--Initial hash values
--(first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19)
atom h0 = 0x6a09e667,
h1 = 0xbb67ae85,
h2 = 0x3c6ef372,
h3 = 0xa54ff53a,
h4 = 0x510e527f,
h5 = 0x9b05688c,
h6 = 0x1f83d9ab,
h7 = 0x5be0cd19
-- add the '1' bit and space for size in bits, padded to multiple of 64
msg &= #80&repeat('\0',pad64(len+8)-len)
len = (len-1)*8
for i=length(msg) to 1 by -1 do
msg[i] = and_bits(len,#FF)
len = floor(len/#100)
if len=0 then exit end if
end for
-- Process the message in successive 512-bit (64 byte) chunks
for chunk=1 to length(msg) by 64 do
for i=1 to 16 do
w[i] = dword(msg,chunk+(i-1)*4)
end for
-- Extend the first 16 words into the remaining 48 words w[17..64] of the message schedule array
for i=17 to 64 do
x = w[i-15]; s0 = xor_bits(xor_bits(ror(x, 7),ror(x,18)),shr(x, 3))
x = w[i-2]; s1 = xor_bits(xor_bits(ror(x,17),ror(x,19)),shr(x,10))
w[i] = uint32(w[i-16]+s0+w[i-7]+s1)
end for
-- Initialize working variables to current hash value
{a,b,c,d,e,f,g,h} = {h0,h1,h2,h3,h4,h5,h6,h7}
-- Compression function main loop
for i=1 to 64 do
s1 = xor_bits(xor_bits(ror(e,6),ror(e,11)),ror(e,25))
ch = xor_bits(and_bits(e,f),and_bits(not_bits(e),g))
temp1 = h+s1+ch+k[i]+w[i]
s0 = xor_bits(xor_bits(ror(a,2),ror(a,13)),ror(a,22))
maj = xor_bits(xor_bits(and_bits(a,b),and_bits(a,c)),and_bits(b,c))
temp2 = s0+maj
{h,g,f,e,d,c,b,a} = sq_uint32({g,f,e,d+temp1,c,b,a,temp1+temp2})
end for
-- Add the compressed chunk to the current hash value
{h0,h1,h2,h3,h4,h5,h6,h7} = sq_add({h0,h1,h2,h3,h4,h5,h6,h7},{a,b,c,d,e,f,g,h})
end for
-- Produce the final hash value (big-endian)
res = sq_uint32({h0, h1, h2, h3, h4, h5, h6, h7}) -- (or do sq_unit32 on the sq_add above)
for i=1 to length(res) do
res[i] = sprintf("%08x",res[i])
end for
return join(res)
end function
string res = sha256("Rosetta code")
?res

View file

@ -0,0 +1,2 @@
var sha = frequire('Digest::SHA');
say sha.sha256_hex('Rosetta code');