|
17 | 17 | using System.Buffers.Binary; |
18 | 18 | using System.Security.Cryptography; |
19 | 19 |
|
20 | | -namespace Neo.FileStorage.API.Cryptography; |
21 | | - |
22 | | -public static class Helper |
| 20 | +namespace Neo.FileStorage.API.Cryptography |
23 | 21 | { |
24 | | - public const int Sha256HashLength = 32; |
25 | | - |
26 | | - internal static byte[] RIPEMD160(this byte[] value) |
27 | | - { |
28 | | - var hash = new byte[20]; |
29 | | - var digest = new RipeMD160Digest(); |
30 | | - digest.BlockUpdate(value, 0, value.Length); |
31 | | - digest.DoFinal(hash, 0); |
32 | | - return hash; |
33 | | - } |
34 | | - |
35 | | - public static byte[] Sha256(this byte[] value) |
36 | | - { |
37 | | - using var sha256 = SHA256.Create(); |
38 | | - return sha256.ComputeHash(value); |
39 | | - } |
40 | | - |
41 | | - internal static byte[] Sha256(this byte[] value, int offset, int count) |
42 | | - { |
43 | | - using var sha256 = SHA256.Create(); |
44 | | - return sha256.ComputeHash(value, offset, count); |
45 | | - } |
46 | | - |
47 | | - internal static byte[] Sha256(this ReadOnlySpan<byte> value) |
| 22 | + public static class Helper |
48 | 23 | { |
49 | | - var buffer = new byte[32]; |
50 | | - using var sha256 = SHA256.Create(); |
51 | | - sha256.TryComputeHash(value, buffer, out _); |
52 | | - return buffer; |
53 | | - } |
54 | | - |
55 | | - public static ByteString Sha256(this IMessage data) |
56 | | - { |
57 | | - return ByteString.CopyFrom(data.ToByteArray().Sha256()); |
58 | | - } |
| 24 | + public const int Sha256HashLength = 32; |
59 | 25 |
|
60 | | - public static ByteString Sha256(this ByteString data) |
61 | | - { |
62 | | - return ByteString.CopyFrom(data.ToByteArray().Sha256()); |
63 | | - } |
| 26 | + internal static byte[] RIPEMD160(this byte[] value) |
| 27 | + { |
| 28 | + var hash = new byte[20]; |
| 29 | + var digest = new RipeMD160Digest(); |
| 30 | + digest.BlockUpdate(value, 0, value.Length); |
| 31 | + digest.DoFinal(hash, 0); |
| 32 | + return hash; |
| 33 | + } |
| 34 | + |
| 35 | + public static byte[] Sha256(this byte[] value) |
| 36 | + { |
| 37 | + using var sha256 = SHA256.Create(); |
| 38 | + return sha256.ComputeHash(value); |
| 39 | + } |
64 | 40 |
|
65 | | - public static Checksum Sha256Checksum(this IMessage data) |
66 | | - { |
67 | | - return new Checksum |
| 41 | + internal static byte[] Sha256(this byte[] value, int offset, int count) |
68 | 42 | { |
69 | | - Type = ChecksumType.Sha256, |
70 | | - Sum = data.Sha256() |
71 | | - }; |
72 | | - } |
| 43 | + using var sha256 = SHA256.Create(); |
| 44 | + return sha256.ComputeHash(value, offset, count); |
| 45 | + } |
73 | 46 |
|
74 | | - public static Checksum Sha256Checksum(this ByteString data) |
75 | | - { |
76 | | - return new Checksum |
| 47 | + internal static byte[] Sha256(this ReadOnlySpan<byte> value) |
77 | 48 | { |
78 | | - Type = ChecksumType.Sha256, |
79 | | - Sum = data.Sha256() |
80 | | - }; |
81 | | - } |
| 49 | + var buffer = new byte[32]; |
| 50 | + using var sha256 = SHA256.Create(); |
| 51 | + sha256.TryComputeHash(value, buffer, out _); |
| 52 | + return buffer; |
| 53 | + } |
82 | 54 |
|
83 | | - public static ByteString TzHash(this IMessage data) |
84 | | - { |
85 | | - return ByteString.CopyFrom(new TzHash().ComputeHash(data.ToByteArray())); |
86 | | - } |
| 55 | + public static ByteString Sha256(this IMessage data) |
| 56 | + { |
| 57 | + return ByteString.CopyFrom(data.ToByteArray().Sha256()); |
| 58 | + } |
87 | 59 |
|
88 | | - public static ByteString TzHash(this ByteString data) |
89 | | - { |
90 | | - return ByteString.CopyFrom(new TzHash().ComputeHash(data.ToByteArray())); |
91 | | - } |
| 60 | + public static ByteString Sha256(this ByteString data) |
| 61 | + { |
| 62 | + return ByteString.CopyFrom(data.ToByteArray().Sha256()); |
| 63 | + } |
92 | 64 |
|
93 | | - public static Checksum TzChecksum(this IMessage data) |
94 | | - { |
95 | | - return new Checksum |
| 65 | + public static Checksum Sha256Checksum(this IMessage data) |
96 | 66 | { |
97 | | - Type = ChecksumType.Sha256, |
98 | | - Sum = data.TzHash() |
99 | | - }; |
100 | | - } |
| 67 | + return new Checksum |
| 68 | + { |
| 69 | + Type = ChecksumType.Sha256, |
| 70 | + Sum = data.Sha256() |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + public static Checksum Sha256Checksum(this ByteString data) |
| 75 | + { |
| 76 | + return new Checksum |
| 77 | + { |
| 78 | + Type = ChecksumType.Sha256, |
| 79 | + Sum = data.Sha256() |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + public static ByteString TzHash(this IMessage data) |
| 84 | + { |
| 85 | + return ByteString.CopyFrom(new TzHash().ComputeHash(data.ToByteArray())); |
| 86 | + } |
101 | 87 |
|
102 | | - public static Checksum TzChecksum(this ByteString data) |
103 | | - { |
104 | | - return new Checksum |
| 88 | + public static ByteString TzHash(this ByteString data) |
105 | 89 | { |
106 | | - Type = ChecksumType.Sha256, |
107 | | - Sum = data.TzHash() |
108 | | - }; |
109 | | - } |
| 90 | + return ByteString.CopyFrom(new TzHash().ComputeHash(data.ToByteArray())); |
| 91 | + } |
110 | 92 |
|
111 | | - public static ulong Murmur64(this byte[] value, uint seed) |
112 | | - { |
113 | | - using var murmur = new Murmur3_128(seed); |
114 | | - return BinaryPrimitives.ReadUInt64LittleEndian(murmur.ComputeHash(value)); |
| 93 | + public static Checksum TzChecksum(this IMessage data) |
| 94 | + { |
| 95 | + return new Checksum |
| 96 | + { |
| 97 | + Type = ChecksumType.Sha256, |
| 98 | + Sum = data.TzHash() |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + public static Checksum TzChecksum(this ByteString data) |
| 103 | + { |
| 104 | + return new Checksum |
| 105 | + { |
| 106 | + Type = ChecksumType.Sha256, |
| 107 | + Sum = data.TzHash() |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + public static ulong Murmur64(this byte[] value, uint seed) |
| 112 | + { |
| 113 | + using var murmur = new Murmur3_128(seed); |
| 114 | + return BinaryPrimitives.ReadUInt64LittleEndian(murmur.ComputeHash(value)); |
| 115 | + } |
115 | 116 | } |
116 | 117 | } |
0 commit comments