Beispiel #1
0
 public static ReadOnlySpan <byte> Hash256(ReadOnlySpan <byte> data)
 {
     using (var sha = new SHA256Managed())
     {
         Span <byte> result = stackalloc byte[32];
         sha.TryComputeHash(data, result, out _);
         sha.TryComputeHash(result, result, out _);
         return(result.ToArray());
     }
 }
Beispiel #2
0
        public static ReadOnlySpan <byte> DoubleSha256(ReadOnlySpan <byte> data)
        {
            using var sha = new SHA256Managed();
            Span <byte> result = new byte[32];

            if (!sha.TryComputeHash(data, result, out _) || !sha.TryComputeHash(result, result, out _))
            {
                ThrowHashGeneratorException($"Failed to perform {nameof(DoubleSha256)}");
            }

            return(result);
        }
Beispiel #3
0
        public static Span <byte> SHA256d(this ReadOnlySpan <byte> data)
        {
            using (var sha = new SHA256Managed())
            {
                //internally this is copied to the destination so its ok to reuse the destination twice
                //https://github.com/dotnet/corefx/blob/57608f6a5cfeadf338dc6c5d0300147b39168012/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/HashAlgorithm.cs#L244
                var h1 = new Span <byte>(new byte[sha.HashSize / 8]);
                sha.TryComputeHash(data, h1, out int _);
                sha.TryComputeHash(h1, h1, out int _);

                return(h1);
            }
        }
        public static UInt256 DoubleSha256AsUInt256(ReadOnlySpan <byte> data)
        {
            using var sha = new SHA256Managed();
            Span <byte> result = stackalloc byte[32];

            if (!sha.TryComputeHash(data, result, out _))
            {
                throw new HashGeneratorException($"Failed to perform {nameof(DoubleSha256AsUInt256)}");
            }
            if (!sha.TryComputeHash(result, result, out _))
            {
                throw new HashGeneratorException($"Failed to perform {nameof(DoubleSha256AsUInt256)}");
            }
            return(new UInt256(result));
        }
Beispiel #5
0
        public static int HashSHA256(ReadOnlySpan <byte> src, Span <byte> dest)
        {
            SHA256 sha = new SHA256Managed();

            if (sha.TryComputeHash(src, dest, out int ret) == false)
            {
                throw new ApplicationException("TryComputeHash error.");
            }

            return(ret);
        }
Beispiel #6
0
        internal string CreateUserHash(HttpContext context)
        {
            ReadOnlySpan <byte> saltSpan = m_saltProvider.GetSalt();

            using IMemoryOwner <byte> uidMemoryOwner = MemoryPool <byte> .Shared.Rent(m_maxIdentitySize + saltSpan.Length);

            Span <byte> uidSpan = uidMemoryOwner.Memory.Span;

            uidSpan.Fill(0);

            int identityBytesWritten = -1;

            foreach (IUserIdentityProvider provider in m_userIdentityProviders)
            {
                if (provider.TryWriteBytes(context, uidSpan.Slice(0, provider.MaxBytesInIdentity), out identityBytesWritten))
                {
                    break;
                }
            }

            if (identityBytesWritten <= 0)
            {
                return(string.Empty);
            }

            saltSpan.CopyTo(uidSpan.Slice(identityBytesWritten));

            using IMemoryOwner <byte> hashMemoryOwner = MemoryPool <byte> .Shared.Rent(HashSize);

            Span <byte> hashSpan = hashMemoryOwner.Memory.Span;

#if NETCOREAPP3_1
            using HashAlgorithm hashAlgorithm = new SHA256Managed();             // need to have new instance each time since its not thread-safe

            if (!hashAlgorithm.TryComputeHash(uidSpan, hashSpan, out _))
            {
                return(string.Empty);
            }

            return(BitConverter.ToString(hashSpan.ToArray()).Replace("-", ""));
#else
            SHA256.HashData(uidSpan, hashSpan);

            return(Convert.ToHexString(hashSpan));
#endif
        }
Beispiel #7
0
 public static Boolean Sha256(ReadOnlySpan <Byte> data, Span <Byte> destination, out Int32 written)
 {
     using SHA256 sha256 = new SHA256Managed();
     return(sha256.TryComputeHash(data, destination, out written));
 }