parent
f6dd0e21c6
commit
fddfa15192
7 changed files with 64 additions and 18 deletions
|
|
@ -19,7 +19,17 @@ namespace System.Security.Cryptography
|
||||||
|
|
||||||
internal static class Argon2
|
internal static class Argon2
|
||||||
{
|
{
|
||||||
internal static Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism,
|
internal static readonly bool IsDarwin = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
||||||
|
internal static readonly bool IsFreeBSD = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
|
||||||
|
internal static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
|
||||||
|
internal static readonly bool IsUnix = IsLinux || IsDarwin || IsFreeBSD;
|
||||||
|
|
||||||
|
internal static readonly IArgon2 Library = IsUnix ? (IArgon2)new UnixArgon2() : new WindowsArgon2();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class WindowsArgon2 : IArgon2
|
||||||
|
{
|
||||||
|
public Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism,
|
||||||
ReadOnlySpan<byte> pwd,
|
ReadOnlySpan<byte> pwd,
|
||||||
ReadOnlySpan<byte> salt,
|
ReadOnlySpan<byte> salt,
|
||||||
Span<byte> hash,
|
Span<byte> hash,
|
||||||
|
|
@ -33,15 +43,15 @@ namespace System.Security.Cryptography
|
||||||
type, version
|
type, version
|
||||||
);
|
);
|
||||||
|
|
||||||
internal static Argon2Error Verify(ReadOnlySpan<byte> encoded, ReadOnlySpan<byte> pwd, int pwdlen, int type) =>
|
public Argon2Error Verify(ReadOnlySpan<byte> encoded, ReadOnlySpan<byte> pwd, int pwdlen, int type) =>
|
||||||
SafeNativeMethods.argon2_verify(in encoded.GetPinnableReference(), in pwd.GetPinnableReference(), pwdlen, type);
|
SafeNativeMethods.argon2_verify(in encoded.GetPinnableReference(), in pwd.GetPinnableReference(), pwdlen, type);
|
||||||
|
|
||||||
internal static Argon2Error Decode(Argon2Context ctx, ReadOnlySpan<byte> str, int type) =>
|
public Argon2Error Decode(Argon2Context ctx, ReadOnlySpan<byte> str, int type) =>
|
||||||
SafeNativeMethods.decode_string(ctx, in str.GetPinnableReference(), type);
|
SafeNativeMethods.decode_string(ctx, in str.GetPinnableReference(), type);
|
||||||
|
|
||||||
internal static class SafeNativeMethods
|
internal static class SafeNativeMethods
|
||||||
{
|
{
|
||||||
[DllImport("libargon2", EntryPoint = "argon2_hash")]
|
[DllImport("argon2", EntryPoint = "argon2_hash", CallingConvention = CallingConvention.Cdecl)]
|
||||||
internal static extern Argon2Error argon2_hash(uint t_cost, uint m_cost, uint parallelism,
|
internal static extern Argon2Error argon2_hash(uint t_cost, uint m_cost, uint parallelism,
|
||||||
in byte pwd, int pwdlen,
|
in byte pwd, int pwdlen,
|
||||||
in byte salt, int saltlen,
|
in byte salt, int saltlen,
|
||||||
|
|
@ -50,10 +60,51 @@ namespace System.Security.Cryptography
|
||||||
int type, int version
|
int type, int version
|
||||||
);
|
);
|
||||||
|
|
||||||
[DllImport("libargon2", EntryPoint = "argon2_verify")]
|
[DllImport("argon2", EntryPoint = "argon2_verify", CallingConvention = CallingConvention.Cdecl)]
|
||||||
internal static extern Argon2Error argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type);
|
internal static extern Argon2Error argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type);
|
||||||
|
|
||||||
[DllImport("libargon2", EntryPoint = "decode_string")]
|
[DllImport("argon2", EntryPoint = "decode_string", CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
internal static extern Argon2Error decode_string(Argon2Context ctx, in byte str, int type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class UnixArgon2 : IArgon2
|
||||||
|
{
|
||||||
|
public Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism,
|
||||||
|
ReadOnlySpan<byte> pwd,
|
||||||
|
ReadOnlySpan<byte> salt,
|
||||||
|
Span<byte> hash,
|
||||||
|
Span<byte> encoded,
|
||||||
|
int type, int version) =>
|
||||||
|
SafeNativeMethods.argon2_hash(t_cost, m_cost, parallelism,
|
||||||
|
in pwd.GetPinnableReference(), pwd.Length,
|
||||||
|
in salt.GetPinnableReference(), salt.Length,
|
||||||
|
ref hash.GetPinnableReference(), hash.Length,
|
||||||
|
ref encoded.GetPinnableReference(), encoded.Length,
|
||||||
|
type, version
|
||||||
|
);
|
||||||
|
|
||||||
|
public Argon2Error Verify(ReadOnlySpan<byte> encoded, ReadOnlySpan<byte> pwd, int pwdlen, int type) =>
|
||||||
|
SafeNativeMethods.argon2_verify(in encoded.GetPinnableReference(), in pwd.GetPinnableReference(), pwdlen, type);
|
||||||
|
|
||||||
|
public Argon2Error Decode(Argon2Context ctx, ReadOnlySpan<byte> str, int type) =>
|
||||||
|
SafeNativeMethods.decode_string(ctx, in str.GetPinnableReference(), type);
|
||||||
|
|
||||||
|
internal static class SafeNativeMethods
|
||||||
|
{
|
||||||
|
[DllImport("argon2", EntryPoint = "argon2_hash")]
|
||||||
|
internal static extern Argon2Error argon2_hash(uint t_cost, uint m_cost, uint parallelism,
|
||||||
|
in byte pwd, int pwdlen,
|
||||||
|
in byte salt, int saltlen,
|
||||||
|
ref byte hash, int hashlen,
|
||||||
|
ref byte encoded, int encodedlen,
|
||||||
|
int type, int version
|
||||||
|
);
|
||||||
|
|
||||||
|
[DllImport("argon2", EntryPoint = "argon2_verify")]
|
||||||
|
internal static extern Argon2Error argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type);
|
||||||
|
|
||||||
|
[DllImport("argon2", EntryPoint = "decode_string")]
|
||||||
internal static extern Argon2Error decode_string(Argon2Context ctx, in byte str, int type);
|
internal static extern Argon2Error decode_string(Argon2Context ctx, in byte str, int type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
|
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
|
||||||
<PackageVersion>1.2.0</PackageVersion>
|
<PackageVersion>1.2.5</PackageVersion>
|
||||||
<RootNamespace>System.Security.Cryptography</RootNamespace>
|
<RootNamespace>System.Security.Cryptography</RootNamespace>
|
||||||
<AssemblyName>Argon2.Bindings</AssemblyName>
|
<AssemblyName>Argon2.Bindings</AssemblyName>
|
||||||
<PlatformTarget>x64</PlatformTarget>
|
<PlatformTarget>x64</PlatformTarget>
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
||||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||||
<Configurations>Debug;Release;Analyze</Configurations>
|
<Configurations>Debug;Release;Analyze</Configurations>
|
||||||
<AssemblyVersion>1.2.0</AssemblyVersion>
|
<AssemblyVersion>1.2.5</AssemblyVersion>
|
||||||
<Platforms>x64</Platforms>
|
<Platforms>x64</Platforms>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
@ -20,11 +20,6 @@
|
||||||
<PackagePath>runtimes/win-x64/native</PackagePath>
|
<PackagePath>runtimes/win-x64/native</PackagePath>
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
<Content Include="runtimes\osx-x64\native\libargon2.dylib">
|
|
||||||
<Pack>true</Pack>
|
|
||||||
<PackagePath>runtimes/osx-x64/native</PackagePath>
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</Content>
|
|
||||||
<Content Include="runtimes\linux-x64\native\libargon2.so">
|
<Content Include="runtimes\linux-x64\native\libargon2.so">
|
||||||
<Pack>true</Pack>
|
<Pack>true</Pack>
|
||||||
<PackagePath>runtimes/linux-x64/native</PackagePath>
|
<PackagePath>runtimes/linux-x64/native</PackagePath>
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ namespace System.Security.Cryptography
|
||||||
Span<byte> passwordBytes = stackalloc byte[StringEncoding.GetByteCount(password)];
|
Span<byte> passwordBytes = stackalloc byte[StringEncoding.GetByteCount(password)];
|
||||||
StringEncoding.GetBytes(password, passwordBytes);
|
StringEncoding.GetBytes(password, passwordBytes);
|
||||||
|
|
||||||
var result = Argon2.Hash(
|
var result = Argon2.Library.Hash(
|
||||||
TimeCost,
|
TimeCost,
|
||||||
MemoryCost,
|
MemoryCost,
|
||||||
Parallelism,
|
Parallelism,
|
||||||
|
|
@ -128,7 +128,7 @@ namespace System.Security.Cryptography
|
||||||
Span<byte> passwordBytes = stackalloc byte[StringEncoding.GetByteCount(password)];
|
Span<byte> passwordBytes = stackalloc byte[StringEncoding.GetByteCount(password)];
|
||||||
StringEncoding.GetBytes(password, passwordBytes);
|
StringEncoding.GetBytes(password, passwordBytes);
|
||||||
|
|
||||||
var result = Argon2.Hash(
|
var result = Argon2.Library.Hash(
|
||||||
TimeCost,
|
TimeCost,
|
||||||
MemoryCost,
|
MemoryCost,
|
||||||
Parallelism,
|
Parallelism,
|
||||||
|
|
@ -172,7 +172,7 @@ namespace System.Security.Cryptography
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Verify(ReadOnlySpan<byte> expectedHash, ReadOnlySpan<byte> password)
|
public bool Verify(ReadOnlySpan<byte> expectedHash, ReadOnlySpan<byte> password)
|
||||||
{
|
{
|
||||||
var result = Argon2.Verify(expectedHash, password, password.Length, (int)ArgonType);
|
var result = Argon2.Library.Verify(expectedHash, password, password.Length, (int)ArgonType);
|
||||||
|
|
||||||
if (result == Argon2Error.OK || result == Argon2Error.VERIFY_MISMATCH || result == Argon2Error.DECODING_FAIL)
|
if (result == Argon2Error.OK || result == Argon2Error.VERIFY_MISMATCH || result == Argon2Error.DECODING_FAIL)
|
||||||
return result == Argon2Error.OK;
|
return result == Argon2Error.OK;
|
||||||
|
|
@ -246,7 +246,7 @@ namespace System.Security.Cryptography
|
||||||
Span<byte> bytes = stackalloc byte[formattedHash.Length];
|
Span<byte> bytes = stackalloc byte[formattedHash.Length];
|
||||||
Encoding.ASCII.GetBytes(formattedHash, bytes);
|
Encoding.ASCII.GetBytes(formattedHash, bytes);
|
||||||
|
|
||||||
var result = Argon2.Decode(context, bytes, (int)type);
|
var result = Argon2.Library.Decode(context, bytes, (int)type);
|
||||||
|
|
||||||
if (result != Argon2Error.OK)
|
if (result != Argon2Error.OK)
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
BIN
Projects/Argon2/runtimes/win-x64/native/libargon2.dll
Normal file → Executable file
BIN
Projects/Argon2/runtimes/win-x64/native/libargon2.dll
Normal file → Executable file
Binary file not shown.
|
|
@ -80,7 +80,7 @@
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Connections.Abstractions" Version="3.1.3" />
|
<PackageReference Include="Microsoft.AspNetCore.Connections.Abstractions" Version="3.1.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.3" />
|
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.3" />
|
||||||
<PackageReference Include="Argon2.Bindings" Version="1.2.0" />
|
<PackageReference Include="Argon2.Bindings" Version="1.2.5" />
|
||||||
<PackageReference Include="Zlib.Bindings" Version="1.0.0" />
|
<PackageReference Include="Zlib.Bindings" Version="1.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup Condition="'$(Configuration)'=='Analyze'">
|
<ItemGroup Condition="'$(Configuration)'=='Analyze'">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue