Reputation: 311185
I've got a ASP.NET 5 RC1 (soon to be ASP.NET Core) Web Application project.
It needs to compute SHA1 hashes.
Various SHA1
subclasses are available and build under DNX 4.5.1, but there doesn't seem to be any available implementation under DNX Core 5.0.
Do I have to add a reference to bring that code in, or is it simply not available for .NET Core yet?
According to this article:
.NET Core consists of a set of libraries, called “CoreFX”, and a small, optimized runtime, called “CoreCLR”.
Sure enough, in the CoreFX repo, there are no subclasses of SHA1
:
However in CoreCLR the subclasses are there as you'd expect, within mscorlib:
Why is there overlap between coreclr and corefx? Is this mscorlib code not available for .NET Core projects?
The description of the System.Security.Crytpography.Algorithms
package on NuGet says:
Provides base types for cryptographic algorithms, including hashing, encryption, and signing operations.
Is there another package that includes actual algorithms and not just base classes? Is this something that simply hasn't been ported yet? Is there somewhere you can review the status of APIs and a roadmap, as Mono has?
Upvotes: 18
Views: 16951
Reputation: 575
string form of Sha1
using System.Security.Cryptography;
using System.Text;
public static string Sha1(string input)
{
using var sha1 = SHA1.Create();
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
Upvotes: 1
Reputation: 56520
Add the System.Security.Cryptography.Algorithms
nuget package.
Then
var sha1 = System.Security.Cryptography.SHA1.Create();
var hash = sha1.ComputeHash(myByteArray)
Upvotes: 38