Mercurial > hg > ucis.core
annotate NaCl/crypto_hash/sha512.cs @ 71:7e9d1cfcc562
NaCl: added ed25519 public key message signing implementation
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Fri, 01 Nov 2013 00:07:36 +0100 |
parents | c873e3dd73fe |
children | b7d981ccd434 |
rev | line source |
---|---|
20 | 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 | |
71
7e9d1cfcc562
NaCl: added ed25519 public key message signing implementation
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
18 public static unsafe void crypto_hash(Byte[] outv, Byte[] inv, int inlen) { |
7e9d1cfcc562
NaCl: added ed25519 public key message signing implementation
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
19 if (outv.Length < 64) throw new ArgumentException("outv.Length < 64"); |
7e9d1cfcc562
NaCl: added ed25519 public key message signing implementation
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
20 if (inv.Length < inlen) throw new ArgumentException("inv.Length < inlen"); |
7e9d1cfcc562
NaCl: added ed25519 public key message signing implementation
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
21 fixed (Byte* outp = outv, inp = inv) crypto_hash(outp, inp, (UInt64)inlen); |
7e9d1cfcc562
NaCl: added ed25519 public key message signing implementation
Ivo Smits <Ivo@UCIS.nl>
parents:
20
diff
changeset
|
22 } |
20 | 23 public static unsafe void crypto_hash(Byte* outp, Byte* inp, UInt64 inlen) { |
24 // Byte[] h = new Byte[64]; | |
25 Byte[] padded = new Byte[256]; | |
26 UInt64 i; | |
27 UInt64 bytes = inlen; | |
28 Byte[] h = new Byte[64] { | |
29 0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08, | |
30 0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b, | |
31 0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b, | |
32 0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1, | |
33 0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1, | |
34 0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f, | |
35 0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b, | |
36 0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79 | |
37 }; | |
38 | |
39 // for (i = 0; i < 64; ++i) h[i] = iv[i]; | |
40 | |
41 fixed (Byte* hp = h) crypto_hashblocks.sha512.crypto_hashblocks(hp, inp, inlen); | |
42 inp += inlen; | |
43 inlen &= 127; | |
44 inp -= inlen; | |
45 | |
46 for (i = 0; i < inlen; ++i) padded[i] = inp[i]; | |
47 padded[inlen] = 0x80; | |
48 | |
49 if (inlen < 112) { | |
50 for (i = inlen + 1; i < 119; ++i) padded[i] = 0; | |
51 padded[119] = (Byte)(bytes >> 61); | |
52 padded[120] = (Byte)(bytes >> 53); | |
53 padded[121] = (Byte)(bytes >> 45); | |
54 padded[122] = (Byte)(bytes >> 37); | |
55 padded[123] = (Byte)(bytes >> 29); | |
56 padded[124] = (Byte)(bytes >> 21); | |
57 padded[125] = (Byte)(bytes >> 13); | |
58 padded[126] = (Byte)(bytes >> 5); | |
59 padded[127] = (Byte)(bytes << 3); | |
60 fixed (Byte* hp = h, paddedp = padded) crypto_hashblocks.sha512.crypto_hashblocks(hp, paddedp, 128); | |
61 } else { | |
62 for (i = inlen + 1; i < 247; ++i) padded[i] = 0; | |
63 padded[247] = (Byte)(bytes >> 61); | |
64 padded[248] = (Byte)(bytes >> 53); | |
65 padded[249] = (Byte)(bytes >> 45); | |
66 padded[250] = (Byte)(bytes >> 37); | |
67 padded[251] = (Byte)(bytes >> 29); | |
68 padded[252] = (Byte)(bytes >> 21); | |
69 padded[253] = (Byte)(bytes >> 13); | |
70 padded[254] = (Byte)(bytes >> 5); | |
71 padded[255] = (Byte)(bytes << 3); | |
72 fixed (Byte* hp = h, paddedp = padded) crypto_hashblocks.sha512.crypto_hashblocks(hp, paddedp, 256); | |
73 } | |
74 | |
75 for (i = 0; i < 64; ++i) outp[i] = h[i]; | |
76 } | |
77 } | |
78 } |