Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
180
Task/RIPEMD-160/FreeBASIC/ripemd-160.freebasic
Normal file
180
Task/RIPEMD-160/FreeBASIC/ripemd-160.freebasic
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
' version 22-10-2016
|
||||
' compile with: fbc -s console
|
||||
|
||||
Function RIPEMD_160(message As String) As String
|
||||
|
||||
#Macro ROtate_left(x, n)
|
||||
(x Shl n Or x Shr (32 - n))
|
||||
#EndMacro
|
||||
|
||||
#Macro f1(x, y, z)
|
||||
(x Xor y Xor z) ' (0 <= j <= 15)
|
||||
|
||||
#EndMacro
|
||||
#Macro f2(x, y, z)
|
||||
((x And y) Or ((Not x) And z)) ' (16 <= j <= 31)
|
||||
|
||||
#EndMacro
|
||||
#Macro f3(x, y, z)
|
||||
((x Or (Not y)) Xor z) ''(32 <= j <= 47)
|
||||
|
||||
#EndMacro
|
||||
#Macro f4(x, y, z)
|
||||
((x And z) Or (y And (Not z))) ''(48 <= j <= 63)
|
||||
|
||||
#EndMacro
|
||||
#Macro f5(x, y, z)
|
||||
(x Xor (y Or (Not z))) ''(64 <= j <= 79)
|
||||
#EndMacro
|
||||
|
||||
Dim As UInteger<32> K(1 To 5), K1(1 To 5)
|
||||
|
||||
K(1) = &H00000000 ' (0 <= j <= 15)
|
||||
K(2) = &H5A827999 ' (16 <= j <= 31)
|
||||
K(3) = &H6ED9EBA1 ' (32 <= j <= 47)
|
||||
K(4) = &H8F1BBCDC ' (48 <= j <= 63)
|
||||
K(5) = &HA953FD4E ' (64 <= j <= 79)
|
||||
K1(1) = &H50A28BE6 ' (0 <= j <= 15)
|
||||
K1(2) = &H5C4DD124 ' (16 <= j <= 31)
|
||||
K1(3) = &H6D703EF3 ' (32 <= j <= 47)
|
||||
K1(4) = &H7A6D76E9 ' (48 <= j <= 63)
|
||||
K1(5) = &H00000000 ' (64 <= j <= 79)
|
||||
|
||||
Dim As UByte r(16 To ...) = _
|
||||
{ 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, _
|
||||
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, _
|
||||
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, _
|
||||
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 }
|
||||
|
||||
Dim As UByte r1(0 To ...) = _
|
||||
{ 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, _
|
||||
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, _
|
||||
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, _
|
||||
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, _
|
||||
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 }
|
||||
|
||||
Dim As UByte s(0 To ...) = _
|
||||
{ 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, _
|
||||
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, _
|
||||
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, _
|
||||
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, _
|
||||
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 }
|
||||
|
||||
Dim As UByte s1(0 To ...) = _
|
||||
{ 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, _
|
||||
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, _
|
||||
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, _
|
||||
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, _
|
||||
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 }
|
||||
|
||||
Dim As UInteger<32> h0 = &H67452301
|
||||
Dim As UInteger<32> h1 = &HEFCDAB89
|
||||
Dim As UInteger<32> h2 = &H98BADCFE
|
||||
Dim As UInteger<32> h3 = &H10325476
|
||||
Dim As UInteger<32> h4 = &HC3D2E1F0
|
||||
|
||||
Dim As Long i, j
|
||||
|
||||
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 -8 + i] = ub_ptr[i]
|
||||
Next
|
||||
|
||||
Dim As UInteger<32> A, B, C, D, E, A1, B1, C1, D1, E1, T, T1
|
||||
|
||||
For i = 0 To (l1 -1) \ 64 ' split into 64 byte block
|
||||
|
||||
' x point to 16 * 4byte block inside the string message
|
||||
Dim As UInteger<32> Ptr X = Cast(UInteger<32> Ptr, @message[i*64])
|
||||
|
||||
A = h0 : B = h1 : C = h2 : D = h3 : E = h4
|
||||
A1 = h0 : B1 = h1 : C1 = h2 : D1 = h3 : E1 = h4
|
||||
|
||||
For j = 0 To 79
|
||||
Select Case As Const j
|
||||
Case 0 To 15
|
||||
T = A + f1(B, C, D) + X[j] '+ K(1)
|
||||
T = ROtate_Left(T, s(j)) + E
|
||||
T1 = A1 + f5(B1, C1, D1) + X[r1(j)] + K1(1)
|
||||
T1 = ROtate_Left(T1, s1(j)) + E1
|
||||
Case 16 To 31
|
||||
T = A + f2(B, C, D) + X[r(j)] + K(2)
|
||||
T = ROtate_Left(T, s(j)) + E
|
||||
T1 = A1 + f4(B1, C1, D1) + X[r1(j)] + K1(2)
|
||||
T1 = ROtate_Left(T1, s1(j)) + E1
|
||||
Case 32 To 47
|
||||
T = A + f3(B, C, D) + X[r(j)] + K(3)
|
||||
T = ROtate_Left(T, s(j)) + E
|
||||
T1 = A1 + f3(B1, C1, D1) + X[r1(j)] + K1(3)
|
||||
T1 = ROtate_Left(T1, s1(j)) + E1
|
||||
Case 48 To 63
|
||||
T = A + f4(B, C, D) + X[r(j)] + K(4)
|
||||
T = ROtate_Left(T, s(j)) + E
|
||||
T1 = A1 + f2(B1, C1, D1) + X[r1(j)] + K1(4)
|
||||
T1 = ROtate_Left(T1, s1(j)) + E1
|
||||
Case 64 To 79
|
||||
T = A + f5(B, C, D) + X[r(j)] + K(5)
|
||||
T = ROtate_Left(T, s(j)) + E
|
||||
T1 = A1 + f1(B1, C1, D1) + X[r1(j)] '+ K1(5)
|
||||
T1 = ROtate_Left(T1, s1(j)) + E1
|
||||
End Select
|
||||
|
||||
A = E : E = D : D = ROtate_Left(C, 10) : C = B : B = T
|
||||
A1 = E1 : E1 = D1 : D1 = ROtate_left(C1, 10) : C1 = B1 : B1 = T1
|
||||
|
||||
Next
|
||||
|
||||
T = h1 + C + D1
|
||||
h1 = h2 + D + E1
|
||||
h2 = h3 + E + A1
|
||||
h3 = h4 + A + B1
|
||||
h4 = h0 + B + C1
|
||||
h0 = T
|
||||
|
||||
Next
|
||||
|
||||
Dim As String answer
|
||||
' convert h0, h1, h2, h3 and h4 in hex, then add, low order first
|
||||
Dim As String hs1 = Hex(h0, 8)
|
||||
For i = 7 To 1 Step -2 : answer += Mid(hs1, i, 2) : Next
|
||||
hs1 = Hex(h1, 8)
|
||||
For i = 7 To 1 Step -2 : answer += Mid(hs1, i, 2) : Next
|
||||
hs1 = Hex(h2, 8)
|
||||
For i = 7 To 1 Step -2 : answer += Mid(hs1, i, 2) : Next
|
||||
hs1 = Hex(h3, 8)
|
||||
For i = 7 To 1 Step -2 : answer += Mid(hs1, i, 2) : Next
|
||||
hs1 = Hex(h4, 8)
|
||||
For i = 7 To 1 Step -2 : answer += Mid(hs1, i, 2) : Next
|
||||
|
||||
Return LCase(answer)
|
||||
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As String test = "Rosetta Code"
|
||||
|
||||
Print
|
||||
Print test; " => "; RIPEMD_160(test)
|
||||
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
1
Task/RIPEMD-160/Lasso/ripemd-160.lasso
Normal file
1
Task/RIPEMD-160/Lasso/ripemd-160.lasso
Normal file
|
|
@ -0,0 +1 @@
|
|||
cipher_digest("Rosetta Code", -digest='RIPEMD160', -hex)
|
||||
197
Task/RIPEMD-160/Swift/ripemd-160-1.swift
Normal file
197
Task/RIPEMD-160/Swift/ripemd-160-1.swift
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
// Circular left shift: http://en.wikipedia.org/wiki/Circular_shift
|
||||
// Precendence should be the same as <<
|
||||
infix operator ~<< { precedence 160 associativity none }
|
||||
|
||||
public func ~<< (lhs: UInt32, rhs: Int) -> UInt32 {
|
||||
return (lhs << UInt32(rhs)) | (lhs >> UInt32(32 - rhs));
|
||||
}
|
||||
|
||||
public struct Block {
|
||||
public init() {}
|
||||
|
||||
var message: [UInt32] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
|
||||
|
||||
// Initial values
|
||||
var h₀: UInt32 = 0x67452301
|
||||
var h₁: UInt32 = 0xEFCDAB89
|
||||
var h₂: UInt32 = 0x98BADCFE
|
||||
var h₃: UInt32 = 0x10325476
|
||||
var h₄: UInt32 = 0xC3D2E1F0
|
||||
|
||||
public var hash: [UInt32] {
|
||||
return [h₀, h₁, h₂, h₃, h₄]
|
||||
}
|
||||
|
||||
// FIXME: Make private as soon as tests support that
|
||||
public mutating func compress (message: [UInt32]) -> () {
|
||||
assert(count(message) == 16, "Wrong message size")
|
||||
|
||||
var Aᴸ = h₀
|
||||
var Bᴸ = h₁
|
||||
var Cᴸ = h₂
|
||||
var Dᴸ = h₃
|
||||
var Eᴸ = h₄
|
||||
|
||||
var Aᴿ = h₀
|
||||
var Bᴿ = h₁
|
||||
var Cᴿ = h₂
|
||||
var Dᴿ = h₃
|
||||
var Eᴿ = h₄
|
||||
|
||||
for j in 0...79 {
|
||||
// Left side
|
||||
let wordᴸ = message[r.Left[j]]
|
||||
let functionᴸ = f(j)
|
||||
|
||||
let Tᴸ: UInt32 = ((Aᴸ &+ functionᴸ(Bᴸ,Cᴸ,Dᴸ) &+ wordᴸ &+ K.Left[j]) ~<< s.Left[j]) &+ Eᴸ
|
||||
|
||||
Aᴸ = Eᴸ
|
||||
Eᴸ = Dᴸ
|
||||
Dᴸ = Cᴸ ~<< 10
|
||||
Cᴸ = Bᴸ
|
||||
Bᴸ = Tᴸ
|
||||
|
||||
// Right side
|
||||
let wordᴿ = message[r.Right[j]]
|
||||
let functionᴿ = f(79 - j)
|
||||
|
||||
let Tᴿ: UInt32 = ((Aᴿ &+ functionᴿ(Bᴿ,Cᴿ,Dᴿ) &+ wordᴿ &+ K.Right[j]) ~<< s.Right[j]) &+ Eᴿ
|
||||
|
||||
Aᴿ = Eᴿ
|
||||
Eᴿ = Dᴿ
|
||||
Dᴿ = Cᴿ ~<< 10
|
||||
Cᴿ = Bᴿ
|
||||
Bᴿ = Tᴿ
|
||||
}
|
||||
|
||||
let T = h₁ &+ Cᴸ &+ Dᴿ
|
||||
h₁ = h₂ &+ Dᴸ &+ Eᴿ
|
||||
h₂ = h₃ &+ Eᴸ &+ Aᴿ
|
||||
h₃ = h₄ &+ Aᴸ &+ Bᴿ
|
||||
h₄ = h₀ &+ Bᴸ &+ Cᴿ
|
||||
h₀ = T
|
||||
}
|
||||
|
||||
public func f (j: Int) -> ((UInt32, UInt32, UInt32) -> UInt32) {
|
||||
switch j {
|
||||
case let index where j < 0:
|
||||
assert(false, "Invalid j")
|
||||
return {(_, _, _) in 0 }
|
||||
case let index where j <= 15:
|
||||
return {(x, y, z) in x ^ y ^ z }
|
||||
case let index where j <= 31:
|
||||
return {(x, y, z) in (x & y) | (~x & z) }
|
||||
case let index where j <= 47:
|
||||
return {(x, y, z) in (x | ~y) ^ z }
|
||||
case let index where j <= 63:
|
||||
return {(x, y, z) in (x & z) | (y & ~z) }
|
||||
case let index where j <= 79:
|
||||
return {(x, y, z) in x ^ (y | ~z) }
|
||||
default:
|
||||
assert(false, "Invalid j")
|
||||
return {(_, _, _) in 0 }
|
||||
}
|
||||
}
|
||||
|
||||
public enum K {
|
||||
case Left, Right
|
||||
|
||||
public subscript(j: Int) -> UInt32 {
|
||||
switch j {
|
||||
case let index where j < 0:
|
||||
assert(false, "Invalid j")
|
||||
return 0
|
||||
case let index where j <= 15:
|
||||
return self == .Left ? 0x00000000 : 0x50A28BE6
|
||||
case let index where j <= 31:
|
||||
return self == .Left ? 0x5A827999 : 0x5C4DD124
|
||||
case let index where j <= 47:
|
||||
return self == .Left ? 0x6ED9EBA1 : 0x6D703EF3
|
||||
case let index where j <= 63:
|
||||
return self == .Left ? 0x8F1BBCDC : 0x7A6D76E9
|
||||
case let index where j <= 79:
|
||||
return self == .Left ? 0xA953FD4E : 0x00000000
|
||||
default:
|
||||
assert(false, "Invalid j")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum r {
|
||||
case Left, Right
|
||||
|
||||
public subscript (j: Int) -> Int {
|
||||
switch j {
|
||||
case let index where j < 0:
|
||||
assert(false, "Invalid j")
|
||||
return 0
|
||||
case let index where j <= 15:
|
||||
if self == .Left {
|
||||
return index
|
||||
} else {
|
||||
return [5,14,7,0,9,2,11,4,13,6,15,8,1,10,3,12][index]
|
||||
}
|
||||
case let index where j <= 31:
|
||||
if self == .Left {
|
||||
return [ 7, 4,13, 1,10, 6,15, 3,12, 0, 9, 5, 2,14,11, 8][index - 16]
|
||||
} else {
|
||||
return [ 6,11, 3, 7, 0,13, 5,10,14,15, 8,12, 4, 9, 1, 2][index - 16]
|
||||
}
|
||||
case let index where j <= 47:
|
||||
if self == .Left {
|
||||
return [3,10,14,4,9,15,8,1,2,7,0,6,13,11,5,12][index - 32]
|
||||
} else {
|
||||
return [15,5,1,3,7,14,6,9,11,8,12,2,10,0,4,13][index - 32]
|
||||
}
|
||||
case let index where j <= 63:
|
||||
if self == .Left {
|
||||
return [1,9,11,10,0,8,12,4,13,3,7,15,14,5,6,2][index - 48]
|
||||
} else {
|
||||
return [8,6,4,1,3,11,15,0,5,12,2,13,9,7,10,14][index - 48]
|
||||
}
|
||||
case let index where j <= 79:
|
||||
if self == .Left {
|
||||
return [ 4,0,5,9,7,12,2,10,14,1,3,8,11,6,15,13][index - 64]
|
||||
} else {
|
||||
return [12,15,10,4,1,5,8,7,6,2,13,14,0,3,9,11][index - 64]
|
||||
}
|
||||
|
||||
default:
|
||||
assert(false, "Invalid j")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public enum s {
|
||||
case Left, Right
|
||||
|
||||
public subscript(j: Int) -> Int {
|
||||
switch j {
|
||||
case let index where j < 0:
|
||||
assert(false, "Invalid j")
|
||||
return 0
|
||||
case let index where j <= 15:
|
||||
return (self == .Left ? [11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8] : [8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6])[j]
|
||||
case let index where j <= 31:
|
||||
return (self == .Left ? [7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12] : [9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11])[j - 16]
|
||||
case let index where j <= 47:
|
||||
return (self == .Left ? [11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5] : [9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5])[j - 32]
|
||||
case let index where j <= 63:
|
||||
return (self == .Left ? [11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12] : [15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8])[j - 48]
|
||||
case let index where j <= 79:
|
||||
return (self == .Left ? [9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6] : [8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11])[j - 64]
|
||||
default:
|
||||
assert(false, "Invalid j")
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
5
Task/RIPEMD-160/Swift/ripemd-160-2.swift
Normal file
5
Task/RIPEMD-160/Swift/ripemd-160-2.swift
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var block = Block()
|
||||
let message:[UInt32] = [ 0x65_73_6f_52, 0x20_61_74_74, 0x65_64_6f_43, 0x00_00_00_80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0 ]
|
||||
block.compress(message)
|
||||
let digest = NSString(format: "%2x%2x%2x%2x%2x", UInt32(bigEndian: block.hash[0]), UInt32(bigEndian: block.hash[1]),UInt32(bigEndian: block.hash[2]), UInt32(bigEndian: block.hash[3]), UInt32(bigEndian: block.hash[4]))
|
||||
println(digest)
|
||||
Loading…
Add table
Add a link
Reference in a new issue