Anonymous user
Main step of GOST 28147-89: Difference between revisions
Replaced the code by something which actually works. Replaced Sboxes by those used in Go implementation. Avoided problems with endianness. Added an example.
m (→{{header|Phix}}: no_empty now defaults to true) |
(Replaced the code by something which actually works. Replaced Sboxes by those used in Go implementation. Avoided problems with endianness. Added an example.) |
||
Line 549:
=={{header|Nim}}==
Algorithm inspired from C, Go, etc.
The Sboxes are computed at compile time.
<lang nim>import sequtils, strutils
k8 = [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7]▼
k7 = [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10]▼
k6 = [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8]▼
k5 = [ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15]▼
k4 = [ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9]▼
k3 = [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11]▼
k2 = [ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1]▼
k1 = [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7]▼
const
proc kboxInit: tuple[k87, k65, k43, k21: array[256, byte]] {.compileTime.} =
for i in 0 .. 255:
result.k87[i] =
result.k65[i] =
result.k43[i] =
result.k21[i] =
const (K87, K65, K43, K21) = kboxInit()
template rol(x: uint32; n: typed): uint32 =
result = x.rol(11)
proc mainStep(input: array[8, byte]; key: array[4, byte]): array[8, byte] =
let input32 = cast[array[2, uint32]](input)
let key = cast[uint32](key)
let val = f(key + input32[0]) xor input32[1]
result[0..3] = cast[array[4, byte]](val)
result[4..7] = input[0..3]
when isMainModule:
const
Input = [byte 0x21, 0x04, 0x3B, 0x04, 0x30, 0x04, 0x32, 0x04]
Key = [byte 0xF9, 0x04, 0xC1, 0xE2]
let output = mainStep(Input, Key)
echo mapIt(output, it.toHex).join(" ")</lang>
{{out}}
▲proc f(x): int64 =
<pre>1F 88 CF 07 21 04 3B 04</pre>
▲ let x = k87[x shr 24 and 255] shl 24 or k65[x shr 16 and 255] shl 16 or
▲ k43[x shr 8 and 255] shl 8 or k21[x and 255]
▲ x shl 11 or x shr (32 - 11)</lang>
=={{header|Perl}}==
|