Cryptography Reference
In-Depth Information
We display these words in hex to allow easy comparison with those in [74]:
> map(bits2hex, Hinit);
["6a09e667", "bb67ae85", "3c6ef372", "a54ff53a", "510e527f", "9b05688c",
"1f83d9ab", "5be0cd19"]
Preprocessing is finished and we are now ready to go into the implementation of
the SHA-256 compression function. The first thing to do is to develop the “message
schedule”, which will be called in each iteration of the compression function, i.e., for
each message block. The message schedule consists of sixty-four 32-bit words (lists
in our case) which are produced by the following function. Note that the remember
option 2 is important here as we are using recursion (the function W calls itself).
> W := proc(block, i::nonnegint)
option remember;
if i <= 15 then
block[1+i]
elif i <= 63 then
Summod2e32(sigma1(W(block,i-2)),W(block,i-7),sigma0(W(block,i-15)),W(block,i-16))
end if
end proc:
The next function, sha256 , is an implementation of SHA-256 that acts on a
message given as a list of bytes and produces a hash value in the same format. We
separate this function from the main one that will act on messages given either as
files or strings because the byte list format is more convenient for later use. This
function closely follows the specification—and the notation—in [74].
> sha256 := proc(message)
local m, N, ms, i, t, a, b, c, d, e, f, g, h, H, T1, T2;
m := ListTools:-Flatten(map(bytetobits, message));
m := preprocess(shapad(m));
N := nops(m);
H := Hinit;
foritoNdo
ms := [seq(W(m[i], t), t=0..63)];
a := H[1]; b := H[2]; c := H[3]; d := H[4];
e := H[5]; f := H[6]; g := H[7]; h := H[8];
for t from 0 to 63 do
T1 := Summod2e32(h, Sigma1(e), Ch(e, f, g), K[t+1], ms[t+1]);
T2 := Summod2e32(Sigma0(a), Maj(a, b, c));
h := g; g := f; f := e; e := Summod2e32(d, T1);
d := c; c := b; b := a; a := Summod2e32(T1, T2)
end do;
H := zip(Summod2e32, [a, b, c, d, e, f, g, h], H)
end do;
map(bits2integer, map(x -> ListTools:-LengthSplit(x, 8), H))
end proc:
Next we give SHA256 , which computes the SHA-256 hash of a message given
as a file, a hex string or a text string. The input parameter messagetype must be
specified to file, hex or text , with the last being the default. The output is a
256-bit hex string.
2 This option lets Maple record in a table the result of each iteration of the procedure, so that it can
be retrieved for successive iterations without having to be recomputed; see Maple's help for details.
Search WWH ::




Custom Search