comparison NaCl/crypto_hash/sha512.cs @ 20:c873e3dd73fe

Added NaCl cryptography code
author Ivo Smits <Ivo@UCIS.nl>
date Mon, 15 Apr 2013 00:43:48 +0200
parents
children 7e9d1cfcc562
comparison
equal deleted inserted replaced
19:b9ef273964fd 20:c873e3dd73fe
1 using System;
2
3 namespace UCIS.NaCl.crypto_hash {
4 public static class sha512 {
5 public static int BYTES = 64;
6
7 /* static Byte[] iv = new Byte[64] {
8 0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08,
9 0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b,
10 0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b,
11 0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1,
12 0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1,
13 0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f,
14 0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b,
15 0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79
16 };*/
17
18 public static unsafe void crypto_hash(Byte* outp, Byte* inp, UInt64 inlen) {
19 // Byte[] h = new Byte[64];
20 Byte[] padded = new Byte[256];
21 UInt64 i;
22 UInt64 bytes = inlen;
23 Byte[] h = new Byte[64] {
24 0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08,
25 0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b,
26 0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b,
27 0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1,
28 0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1,
29 0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f,
30 0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b,
31 0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79
32 };
33
34 // for (i = 0; i < 64; ++i) h[i] = iv[i];
35
36 fixed (Byte* hp = h) crypto_hashblocks.sha512.crypto_hashblocks(hp, inp, inlen);
37 inp += inlen;
38 inlen &= 127;
39 inp -= inlen;
40
41 for (i = 0; i < inlen; ++i) padded[i] = inp[i];
42 padded[inlen] = 0x80;
43
44 if (inlen < 112) {
45 for (i = inlen + 1; i < 119; ++i) padded[i] = 0;
46 padded[119] = (Byte)(bytes >> 61);
47 padded[120] = (Byte)(bytes >> 53);
48 padded[121] = (Byte)(bytes >> 45);
49 padded[122] = (Byte)(bytes >> 37);
50 padded[123] = (Byte)(bytes >> 29);
51 padded[124] = (Byte)(bytes >> 21);
52 padded[125] = (Byte)(bytes >> 13);
53 padded[126] = (Byte)(bytes >> 5);
54 padded[127] = (Byte)(bytes << 3);
55 fixed (Byte* hp = h, paddedp = padded) crypto_hashblocks.sha512.crypto_hashblocks(hp, paddedp, 128);
56 } else {
57 for (i = inlen + 1; i < 247; ++i) padded[i] = 0;
58 padded[247] = (Byte)(bytes >> 61);
59 padded[248] = (Byte)(bytes >> 53);
60 padded[249] = (Byte)(bytes >> 45);
61 padded[250] = (Byte)(bytes >> 37);
62 padded[251] = (Byte)(bytes >> 29);
63 padded[252] = (Byte)(bytes >> 21);
64 padded[253] = (Byte)(bytes >> 13);
65 padded[254] = (Byte)(bytes >> 5);
66 padded[255] = (Byte)(bytes << 3);
67 fixed (Byte* hp = h, paddedp = padded) crypto_hashblocks.sha512.crypto_hashblocks(hp, paddedp, 256);
68 }
69
70 for (i = 0; i < 64; ++i) outp[i] = h[i];
71 }
72 }
73 }