Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
saucecontrol committed Jun 13, 2020
1 parent e7be2e2 commit 65d658f
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 106 deletions.
1 change: 1 addition & 0 deletions build/Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

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

Expand Down
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2018-2019 Clinton Ingram
Copyright (c) 2018-2020 Clinton Ingram

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
170 changes: 94 additions & 76 deletions readme.md

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions src/Blake2Fast/Blake2b/Blake2bHashState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ internal void Init(int digestLength = HashBytes, ReadOnlySpan<byte> key = defaul
}
}

/// <inheritdoc />
public void Update(ReadOnlySpan<byte> input)
private void update(ReadOnlySpan<byte> input)
{
if (outlen == 0) ThrowHelper.HashNotInitialized();
if (f[0] != 0) ThrowHelper.HashFinalized();
Expand Down Expand Up @@ -156,9 +155,18 @@ public void Update<T>(ReadOnlySpan<T> input) where T : struct
{
ThrowHelper.ThrowIfIsRefOrContainsRefs<T>();

Update(MemoryMarshal.AsBytes(input));
update(MemoryMarshal.AsBytes(input));
}

/// <inheritdoc />
public void Update<T>(Span<T> input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(ArraySegment<T> input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(T[] input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(T input) where T : struct
{
Expand All @@ -169,9 +177,9 @@ public void Update<T>(T input) where T : struct
#if BUILTIN_SPAN
Update(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<T, byte>(ref input), Unsafe.SizeOf<T>()));
#else
Span<byte> buff = stackalloc byte[Unsafe.SizeOf<T>()];
var buff = (Span<byte>)stackalloc byte[Unsafe.SizeOf<T>()];
Unsafe.WriteUnaligned(ref buff[0], input);
Update(buff);
update(buff);
#endif
return;
}
Expand Down
18 changes: 13 additions & 5 deletions src/Blake2Fast/Blake2s/Blake2sHashState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ internal void Init(int digestLength = HashBytes, ReadOnlySpan<byte> key = defaul
}
}

/// <inheritdoc />
public void Update(ReadOnlySpan<byte> input)
private void update(ReadOnlySpan<byte> input)
{
if (outlen == 0) ThrowHelper.HashNotInitialized();
if (f[0] != 0) ThrowHelper.HashFinalized();
Expand Down Expand Up @@ -151,9 +150,18 @@ public void Update<T>(ReadOnlySpan<T> input) where T : struct
{
ThrowHelper.ThrowIfIsRefOrContainsRefs<T>();

Update(MemoryMarshal.AsBytes(input));
update(MemoryMarshal.AsBytes(input));
}

/// <inheritdoc />
public void Update<T>(Span<T> input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(ArraySegment<T> input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(T[] input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(T input) where T : struct
{
Expand All @@ -164,9 +172,9 @@ public void Update<T>(T input) where T : struct
#if BUILTIN_SPAN
Update(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<T, byte>(ref input), Unsafe.SizeOf<T>()));
#else
Span<byte> buff = stackalloc byte[Unsafe.SizeOf<T>()];
var buff = (Span<byte>)stackalloc byte[Unsafe.SizeOf<T>()];
Unsafe.WriteUnaligned(ref buff[0], input);
Update(buff);
update(buff);
#endif
return;
}
Expand Down
17 changes: 12 additions & 5 deletions src/Blake2Fast/IBlake2Incremental.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,27 @@ interface IBlake2Incremental
/// <summary>The hash digest length for this instance, in bytes.</summary>
int DigestLength { get; }

/// <summary>Update the hash state with the bytes contained in <paramref name="input" />.</summary>
/// <summary>Update the hash state with the bytes of all values in <paramref name="input" />.</summary>
/// <param name="input">The message bytes to add to the hash state.</param>
void Update(ReadOnlySpan<byte> input);

/// <inheritdoc cref="Update(ReadOnlySpan{byte})" />
/// <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>
/// <remarks>
/// The <typeparamref name="T" /> value will be added to the hash state in memory layout order, including any padding bytes.
/// 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.
/// <see cref="byte"/> is the only type that is guaranteed to be consistent across platforms.
/// </remarks>
/// <exception cref="NotSupportedException">Thrown when <typeparamref name="T"/> is a reference type or contains any fields of reference types.</exception>
void Update<T>(ReadOnlySpan<T> input) where T : struct;

/// <summary>Update the hash state with the <paramref name="input" /> value.</summary>
/// <inheritdoc cref="Update{T}(ReadOnlySpan{T})" />
void Update<T>(Span<T> input) where T : struct;

/// <inheritdoc cref="Update{T}(ReadOnlySpan{T})" />
void Update<T>(ArraySegment<T> input) where T : struct;

/// <inheritdoc cref="Update{T}(ReadOnlySpan{T})" />
void Update<T>(T[] input) where T : struct;

/// <summary>Update the hash state with the bytes of the <paramref name="input" /> value.</summary>
/// <inheritdoc cref="Update{T}(ReadOnlySpan{T})" />
void Update<T>(T input) where T : struct;

Expand Down
18 changes: 13 additions & 5 deletions src/Blake2Fast/_Blake2Main.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ if (alg.bits == 64) {
}
}

/// <inheritdoc />
public void Update(ReadOnlySpan<byte> input)
private void update(ReadOnlySpan<byte> input)
{
if (outlen == 0) ThrowHelper.HashNotInitialized();
if (f[0] != 0) ThrowHelper.HashFinalized();
Expand Down Expand Up @@ -231,9 +230,18 @@ if (alg.bits == 64) {
{
ThrowHelper.ThrowIfIsRefOrContainsRefs<T>();

Update(MemoryMarshal.AsBytes(input));
update(MemoryMarshal.AsBytes(input));
}

/// <inheritdoc />
public void Update<T>(Span<T> input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(ArraySegment<T> input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(T[] input) where T : struct => Update((ReadOnlySpan<T>)input);

/// <inheritdoc />
public void Update<T>(T input) where T : struct
{
Expand All @@ -244,9 +252,9 @@ if (alg.bits == 64) {
#if BUILTIN_SPAN
Update(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<T, byte>(ref input), Unsafe.SizeOf<T>()));
#else
Span<byte> buff = stackalloc byte[Unsafe.SizeOf<T>()];
var buff = (Span<byte>)stackalloc byte[Unsafe.SizeOf<T>()];
Unsafe.WriteUnaligned(ref buff[0], input);
Update(buff);
update(buff);
#endif
return;
}
Expand Down
18 changes: 9 additions & 9 deletions tests/Blake2.Test/KnownAnswerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ private static byte[] getBytes(string s)

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

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

public static readonly KatEntry[] All = getAllKat();
public static readonly KatEntry[] Values = getKatValues();

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

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

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

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

public class KnownAnswerTest
Expand Down Expand Up @@ -139,12 +139,12 @@ public void KatBlake2sKeyed(KatEntry ka)
[Fact]
public void UpdateThrowsOnRefContainingT()
{
Assert.Throws<NotSupportedException>(() => Blake2b.CreateIncrementalHasher().Update(KatEntry.All[0]));
Assert.Throws<NotSupportedException>(() => Blake2b.CreateIncrementalHasher().Update(KatEntry.Values.First()));
}

[Fact]
public void UpdateThrowsOnRefContainingSpanT()
{
Assert.Throws<NotSupportedException>(() => Blake2b.CreateIncrementalHasher().Update(new ReadOnlySpan<KatEntry>(KatEntry.All)));
Assert.Throws<NotSupportedException>(() => Blake2b.CreateIncrementalHasher().Update(KatEntry.Values));
}
}

0 comments on commit 65d658f

Please sign in to comment.