IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
SHA1HashWriter.cs
Go to the documentation of this file.
1using System;
2using System.Buffers;
3using System.Buffers.Binary;
4using System.Security.Cryptography;
5
7{
8
15 readonly struct SHA1HashWriter : IDisposable
16 {
17
18 readonly IncrementalHash hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1);
19#if NETFRAMEWORK
20 readonly byte[] temp = ArrayPool<byte>.Shared.Rent(1024);
21#endif
22
27 {
28
29 }
30
31 public void WriteUInt16(ushort s)
32 {
33#if NETFRAMEWORK
34 BinaryPrimitives.WriteUInt16BigEndian(temp, s);
35 hash.AppendData(temp, 0, 2);
36#else
37 var buf = (Span<byte>)stackalloc byte[2];
38 BinaryPrimitives.WriteUInt16BigEndian(buf, s);
39 hash.AppendData(buf);
40#endif
41 }
42
43 public void WriteUInt32(uint u)
44 {
45#if NETFRAMEWORK
46 BinaryPrimitives.WriteUInt32BigEndian(temp, u);
47 hash.AppendData(temp, 0, 4);
48#else
49 var span = (Span<byte>)stackalloc byte[4];
50 BinaryPrimitives.WriteUInt32BigEndian(span, u);
51 hash.AppendData(span);
52#endif
53 }
54
55 public void WriteInt64(long l)
56 {
57#if NETFRAMEWORK
58 BinaryPrimitives.WriteInt64BigEndian(temp, l);
59 hash.AppendData(temp, 0, 8);
60#else
61 var span = (Span<byte>)stackalloc byte[8];
62 BinaryPrimitives.WriteInt64BigEndian(span, l);
63 hash.AppendData(span);
64#endif
65 }
66
67 public void WriteFloat(float f)
68 {
69 WriteUInt32(BitConverter.ToUInt32(BitConverter.GetBytes(f), 0));
70 }
71
72 public void WriteDouble(double d)
73 {
74 WriteInt64(BitConverter.ToInt64(BitConverter.GetBytes(d), 0));
75 }
76
77 public void WriteByte(byte b)
78 {
79#if NETFRAMEWORK
80 temp[0] = b;
81 hash.AppendData(temp, 0, 1);
82#else
83 var span = (Span<byte>)stackalloc byte[1];
84 span[0] = b;
85 hash.AppendData(span);
86#endif
87 }
88
89 public void WriteBytes(byte[] data)
90 {
91 hash.AppendData(data);
92 }
93
94 public void WriteUtf8(string str)
95 {
96 var buf = ArrayPool<byte>.Shared.Rent(str.Length * 3 + 1);
97
98 try
99 {
100 int j = 0;
101 for (int i = 0, e = str.Length; i < e; i++)
102 {
103 char ch = str[i];
104 if ((ch != 0) && (ch <= 0x7f))
105 {
106 buf[j++] = (byte)ch;
107 }
108 else if (ch <= 0x7FF)
109 {
110 /* 11 bits or less. */
111 var high_five = (byte)(ch >> 6);
112 var low_six = (byte)(ch & 0x3F);
113 buf[j++] = (byte)(high_five | 0xC0); /* 110xxxxx */
114 buf[j++] = (byte)(low_six | 0x80); /* 10xxxxxx */
115 }
116 else
117 {
118 /* possibly full 16 bits. */
119 var high_four = (byte)(ch >> 12);
120 var mid_six = (byte)((ch >> 6) & 0x3F);
121 var low_six = (byte)(ch & 0x3f);
122 buf[j++] = (byte)(high_four | 0xE0); /* 1110xxxx */
123 buf[j++] = (byte)(mid_six | 0x80); /* 10xxxxxx */
124 buf[j++] = (byte)(low_six | 0x80); /* 10xxxxxx*/
125 }
126 }
127
128 WriteUInt16((ushort)j);
129 hash.AppendData(buf, 0, j);
130 }
131 finally
132 {
133 ArrayPool<byte>.Shared.Return(buf);
134 }
135 }
136
141 public byte[] Finalize()
142 {
143 return hash.GetHashAndReset();
144 }
145
149 public void Dispose()
150 {
151#if NETFRAMEWORK
152 ArrayPool<byte>.Shared.Return(temp);
153#endif
154 }
155
156 }
157
158}
Provides methods to write basic building blocks to an incremental hash.
byte[] Finalize()
Finalizes the SHA1.
readonly IncrementalHash hash
void Dispose()
Disposes of the instance.
SHA1HashWriter()
Initializes a new instance.