Skip to content

Commit 65d658f

Browse files
committed
v2.0.0
1 parent e7be2e2 commit 65d658f

File tree

8 files changed

+156
-106
lines changed

8 files changed

+156
-106
lines changed

build/Common.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
<ItemGroup Condition="'$(Configuration)'=='Dist' Or '$(Configuration)'=='Coverage'">
5050
<None Include="$(MSBuildThisFileDirectory)$(Owners).png" Pack="true" PackagePath="package" />
51+
<None Include="$(RepositoryRoot)license" Pack="true" PackagePath="package" />
5152
<SourceRoot Include="$(RepositoryRoot)" />
5253
</ItemGroup>
5354

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License
22

3-
Copyright (c) 2018-2019 Clinton Ingram
3+
Copyright (c) 2018-2020 Clinton Ingram
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

readme.md

Lines changed: 94 additions & 76 deletions
Large diffs are not rendered by default.

src/Blake2Fast/Blake2b/Blake2bHashState.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ internal void Init(int digestLength = HashBytes, ReadOnlySpan<byte> key = defaul
114114
}
115115
}
116116

117-
/// <inheritdoc />
118-
public void Update(ReadOnlySpan<byte> input)
117+
private void update(ReadOnlySpan<byte> input)
119118
{
120119
if (outlen == 0) ThrowHelper.HashNotInitialized();
121120
if (f[0] != 0) ThrowHelper.HashFinalized();
@@ -156,9 +155,18 @@ public void Update<T>(ReadOnlySpan<T> input) where T : struct
156155
{
157156
ThrowHelper.ThrowIfIsRefOrContainsRefs<T>();
158157

159-
Update(MemoryMarshal.AsBytes(input));
158+
update(MemoryMarshal.AsBytes(input));
160159
}
161160

161+
/// <inheritdoc />
162+
public void Update<T>(Span<T> input) where T : struct => Update((ReadOnlySpan<T>)input);
163+
164+
/// <inheritdoc />
165+
public void Update<T>(ArraySegment<T> input) where T : struct => Update((ReadOnlySpan<T>)input);
166+
167+
/// <inheritdoc />
168+
public void Update<T>(T[] input) where T : struct => Update((ReadOnlySpan<T>)input);
169+
162170
/// <inheritdoc />
163171
public void Update<T>(T input) where T : struct
164172
{
@@ -169,9 +177,9 @@ public void Update<T>(T input) where T : struct
169177
#if BUILTIN_SPAN
170178
Update(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<T, byte>(ref input), Unsafe.SizeOf<T>()));
171179
#else
172-
Span<byte> buff = stackalloc byte[Unsafe.SizeOf<T>()];
180+
var buff = (Span<byte>)stackalloc byte[Unsafe.SizeOf<T>()];
173181
Unsafe.WriteUnaligned(ref buff[0], input);
174-
Update(buff);
182+
update(buff);
175183
#endif
176184
return;
177185
}

src/Blake2Fast/Blake2s/Blake2sHashState.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ internal void Init(int digestLength = HashBytes, ReadOnlySpan<byte> key = defaul
109109
}
110110
}
111111

112-
/// <inheritdoc />
113-
public void Update(ReadOnlySpan<byte> input)
112+
private void update(ReadOnlySpan<byte> input)
114113
{
115114
if (outlen == 0) ThrowHelper.HashNotInitialized();
116115
if (f[0] != 0) ThrowHelper.HashFinalized();
@@ -151,9 +150,18 @@ public void Update<T>(ReadOnlySpan<T> input) where T : struct
151150
{
152151
ThrowHelper.ThrowIfIsRefOrContainsRefs<T>();
153152

154-
Update(MemoryMarshal.AsBytes(input));
153+
update(MemoryMarshal.AsBytes(input));
155154
}
156155

156+
/// <inheritdoc />
157+
public void Update<T>(Span<T> input) where T : struct => Update((ReadOnlySpan<T>)input);
158+
159+
/// <inheritdoc />
160+
public void Update<T>(ArraySegment<T> input) where T : struct => Update((ReadOnlySpan<T>)input);
161+
162+
/// <inheritdoc />
163+
public void Update<T>(T[] input) where T : struct => Update((ReadOnlySpan<T>)input);
164+
157165
/// <inheritdoc />
158166
public void Update<T>(T input) where T : struct
159167
{
@@ -164,9 +172,9 @@ public void Update<T>(T input) where T : struct
164172
#if BUILTIN_SPAN
165173
Update(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<T, byte>(ref input), Unsafe.SizeOf<T>()));
166174
#else
167-
Span<byte> buff = stackalloc byte[Unsafe.SizeOf<T>()];
175+
var buff = (Span<byte>)stackalloc byte[Unsafe.SizeOf<T>()];
168176
Unsafe.WriteUnaligned(ref buff[0], input);
169-
Update(buff);
177+
update(buff);
170178
#endif
171179
return;
172180
}

src/Blake2Fast/IBlake2Incremental.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,27 @@ interface IBlake2Incremental
2626
/// <summary>The hash digest length for this instance, in bytes.</summary>
2727
int DigestLength { get; }
2828

29-
/// <summary>Update the hash state with the bytes contained in <paramref name="input" />.</summary>
29+
/// <summary>Update the hash state with the bytes of all values in <paramref name="input" />.</summary>
3030
/// <param name="input">The message bytes to add to the hash state.</param>
31-
void Update(ReadOnlySpan<byte> input);
32-
33-
/// <inheritdoc cref="Update(ReadOnlySpan{byte})" />
3431
/// <typeparam name="T">The type of the data that will be added to the hash state. It must be a value type and must not contain any reference type fields.</typeparam>
3532
/// <remarks>
3633
/// The <typeparamref name="T" /> value will be added to the hash state in memory layout order, including any padding bytes.
3734
/// Use caution when using this overload with non-primitive structs or when hash values are to be compared across machines with different struct layouts or byte orders.
35+
/// <see cref="byte"/> is the only type that is guaranteed to be consistent across platforms.
3836
/// </remarks>
3937
/// <exception cref="NotSupportedException">Thrown when <typeparamref name="T"/> is a reference type or contains any fields of reference types.</exception>
4038
void Update<T>(ReadOnlySpan<T> input) where T : struct;
4139

42-
/// <summary>Update the hash state with the <paramref name="input" /> value.</summary>
40+
/// <inheritdoc cref="Update{T}(ReadOnlySpan{T})" />
41+
void Update<T>(Span<T> input) where T : struct;
42+
43+
/// <inheritdoc cref="Update{T}(ReadOnlySpan{T})" />
44+
void Update<T>(ArraySegment<T> input) where T : struct;
45+
46+
/// <inheritdoc cref="Update{T}(ReadOnlySpan{T})" />
47+
void Update<T>(T[] input) where T : struct;
48+
49+
/// <summary>Update the hash state with the bytes of the <paramref name="input" /> value.</summary>
4350
/// <inheritdoc cref="Update{T}(ReadOnlySpan{T})" />
4451
void Update<T>(T input) where T : struct;
4552

src/Blake2Fast/_Blake2Main.ttinclude

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ if (alg.bits == 64) {
189189
}
190190
}
191191

192-
/// <inheritdoc />
193-
public void Update(ReadOnlySpan<byte> input)
192+
private void update(ReadOnlySpan<byte> input)
194193
{
195194
if (outlen == 0) ThrowHelper.HashNotInitialized();
196195
if (f[0] != 0) ThrowHelper.HashFinalized();
@@ -231,9 +230,18 @@ if (alg.bits == 64) {
231230
{
232231
ThrowHelper.ThrowIfIsRefOrContainsRefs<T>();
233232

234-
Update(MemoryMarshal.AsBytes(input));
233+
update(MemoryMarshal.AsBytes(input));
235234
}
236235

236+
/// <inheritdoc />
237+
public void Update<T>(Span<T> input) where T : struct => Update((ReadOnlySpan<T>)input);
238+
239+
/// <inheritdoc />
240+
public void Update<T>(ArraySegment<T> input) where T : struct => Update((ReadOnlySpan<T>)input);
241+
242+
/// <inheritdoc />
243+
public void Update<T>(T[] input) where T : struct => Update((ReadOnlySpan<T>)input);
244+
237245
/// <inheritdoc />
238246
public void Update<T>(T input) where T : struct
239247
{
@@ -244,9 +252,9 @@ if (alg.bits == 64) {
244252
#if BUILTIN_SPAN
245253
Update(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<T, byte>(ref input), Unsafe.SizeOf<T>()));
246254
#else
247-
Span<byte> buff = stackalloc byte[Unsafe.SizeOf<T>()];
255+
var buff = (Span<byte>)stackalloc byte[Unsafe.SizeOf<T>()];
248256
Unsafe.WriteUnaligned(ref buff[0], input);
249-
Update(buff);
257+
update(buff);
250258
#endif
251259
return;
252260
}

tests/Blake2.Test/KnownAnswerTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ private static byte[] getBytes(string s)
3838

3939
private static int getDigit(char c) => c < 'a' ? c - '0' : c - 'a' + 10;
4040

41-
private static KatEntry[] getAllKat()
41+
private static KatEntry[] getKatValues()
4242
{
43-
using var stm = typeof(KnownAnswerTest).Assembly.GetManifestResourceStream("Blake2.Test.blake2-kat.json");
43+
using var stm = typeof(KatEntry).Assembly.GetManifestResourceStream("Blake2.Test.blake2-kat.json");
4444
return ((JsonArray)JsonValue.Load(stm)).Cast<JsonObject>().Select(o => new KatEntry(o)).ToArray();
4545
}
4646

47-
public static readonly KatEntry[] All = getAllKat();
47+
public static readonly KatEntry[] Values = getKatValues();
4848

49-
public static IEnumerable<object[]> Blake2b => All.Where(k => k.Alg == "blake2b" && k.Key.Length == 0).Select(k => new object[] { k });
49+
public static IEnumerable<object[]> Blake2b => Values.Where(k => k.Alg == "blake2b" && k.Key.Length == 0).Select(k => new object[] { k });
5050

51-
public static IEnumerable<object[]> Blake2bKeyed => All.Where(k => k.Alg == "blake2b" && k.Key.Length != 0).Select(k => new object[] { k });
51+
public static IEnumerable<object[]> Blake2bKeyed => Values.Where(k => k.Alg == "blake2b" && k.Key.Length != 0).Select(k => new object[] { k });
5252

53-
public static IEnumerable<object[]> Blake2s => All.Where(k => k.Alg == "blake2s" && k.Key.Length == 0).Select(k => new object[] { k });
53+
public static IEnumerable<object[]> Blake2s => Values.Where(k => k.Alg == "blake2s" && k.Key.Length == 0).Select(k => new object[] { k });
5454

55-
public static IEnumerable<object[]> Blake2sKeyed => All.Where(k => k.Alg == "blake2s" && k.Key.Length != 0).Select(k => new object[] { k });
55+
public static IEnumerable<object[]> Blake2sKeyed => Values.Where(k => k.Alg == "blake2s" && k.Key.Length != 0).Select(k => new object[] { k });
5656
}
5757

5858
public class KnownAnswerTest
@@ -139,12 +139,12 @@ public void KatBlake2sKeyed(KatEntry ka)
139139
[Fact]
140140
public void UpdateThrowsOnRefContainingT()
141141
{
142-
Assert.Throws<NotSupportedException>(() => Blake2b.CreateIncrementalHasher().Update(KatEntry.All[0]));
142+
Assert.Throws<NotSupportedException>(() => Blake2b.CreateIncrementalHasher().Update(KatEntry.Values.First()));
143143
}
144144

145145
[Fact]
146146
public void UpdateThrowsOnRefContainingSpanT()
147147
{
148-
Assert.Throws<NotSupportedException>(() => Blake2b.CreateIncrementalHasher().Update(new ReadOnlySpan<KatEntry>(KatEntry.All)));
148+
Assert.Throws<NotSupportedException>(() => Blake2b.CreateIncrementalHasher().Update(KatEntry.Values));
149149
}
150150
}

0 commit comments

Comments
 (0)