Jump to content

Main step of GOST 28147-89: Difference between revisions

Updated D entry
(Updated D entry)
(Updated D entry)
Line 124:
{{trans|C}}
{{trans|Go}}
<lang d>///import Rotate uintstd.stdio, leftstd.range;
 
uint rol(in uint x, in uint nBits) @safe pure nothrow {
/// Rotate uint left.
uint rol(in uint x, in uint nBits) @safe pure nothrow @nogc {
return (x << nBits) | (x >> (32 - nBits));
}
Line 132 ⟶ 134:
alias SBox = immutable Nibble[16][8];
 
private bool _validateSBox(in SBox data) @safe pure nothrow @nogc {
foreach (const ref row; data)
foreach (ub; row)
if (ub >= 16) // Verify it's a nibble.
Line 142 ⟶ 144:
struct GOST(s...) if (s.length == 1 && s[0]._validateSBox) {
private static generate(ubyte k)() @safe pure nothrow {
//return iota(k87.length).iota
// .map!(i=> (s[0][k][i >> 4] << 4) | s[0][k - 1][i & 0xF])
// .array;
uint[k87.length] result; // ubytes[...] should suffice.
foreach (immutable i, ref item; result)
item = (s[0][k][i >> 4] << 4) | s[0][k - 1][i & 0xF];
return result;
}
 
Line 158 ⟶ 156:
 
// Endianess problems?
private static uint f(in uint x) pure nothrow @nogc @safe {
immutable uint y = (k87[(x >> 24) & 0xFF] << 24) |
(k65[(x >> 16) & 0xFF] << 16) |
Line 167 ⟶ 165:
 
// This performs only a step of the encoding.
public void mainStep(in uint[2] input, in uint key) pure nothrow {
pure nothrow @nogc @safe {
buffer[0] = f(key + input[0]) ^ input[1];
buffer[1] = input[0];
Line 174 ⟶ 173:
 
void main() {
import std.stdio;
 
// S-boxes used by the Central Bank of Russian Federation:
// http://en.wikipedia.org/wiki/GOST_28147-89
Cookies help us deliver our services. By using our services, you agree to our use of cookies.