Exemple #1
0
 public static byte[] Sha256Digest(this byte[] bytes)
 {
     Org.BouncyCastle.Crypto.Digests.Sha256Digest myHash = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
     myHash.BlockUpdate(bytes, 0, bytes.Length);
     byte[] compArr = new byte[myHash.GetDigestSize()];
     myHash.DoFinal(compArr, 0);
     return(compArr);
 }
Exemple #2
0
        public static byte[] SHA256(string text)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);

            Org.BouncyCastle.Crypto.Digests.Sha256Digest digester = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
            byte[] retValue = new byte[digester.GetDigestSize()];
            digester.BlockUpdate(bytes, 0, bytes.Length);
            digester.DoFinal(retValue, 0);
            return(retValue);
        }
Exemple #3
0
        public BigInteger GetHash(int n)
        {
            var encData = BitConverter.GetBytes(n);

            myHash.BlockUpdate(encData, 0, encData.Length);
            byte[] compArr = new byte[myHash.GetDigestSize()];
            myHash.DoFinal(compArr, 0);
            var x = compArr.ToList();

            x.Add(0);
            return(new BigInteger(x.ToArray()));
        }
Exemple #4
0
 public static byte[] SimpleHash(byte[] data, byte[] addition = null)
 {
     Org.BouncyCastle.Crypto.Digests.Sha256Digest myHash = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
     myHash.BlockUpdate(data, 0, data.Length);
     if (addition != null)
     {
         myHash.BlockUpdate(addition, 0, addition.Length);
     }
     byte[] compArr = new byte[myHash.GetDigestSize()];
     myHash.DoFinal(compArr, 0);
     return(compArr);
 }
        /// <summary>
        /// Calculate a SHA-256 digest (hash).
        /// </summary>
        /// <param name="data">The data to be hashed.</param>
        /// <returns>A byte array that contains the calculated hash value.</returns>
        static protected byte[] Sha256Digest(byte[] data)
        {
            var hasher = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();

            hasher.BlockUpdate(data, 0, data.Length);

            var digest = new byte[hasher.GetDigestSize()];

            hasher.DoFinal(digest, 0);

            return(digest);
        }
Exemple #6
0
        protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
        {
            System.Console.WriteLine(data);

            return(data);

            // byte[] data = System.Text.Encoding.UTF8.GetBytes(text);

            Org.BouncyCastle.Crypto.Digests.Sha256Digest digester = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
            byte[] retValue = new byte[digester.GetDigestSize()];
            digester.BlockUpdate(data, 0, data.Length);
            digester.DoFinal(retValue, 0);
            return(retValue);
        }
Exemple #7
0
        private string CreateSHA256Hash(string data)
        {
            var encData = Encoding.UTF8.GetBytes(data);

            Org.BouncyCastle.Crypto.Digests.Sha256Digest myHash = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
            myHash.BlockUpdate(encData, 0, encData.Length);
            byte[] compArr = new byte[myHash.GetDigestSize()];
            myHash.DoFinal(compArr, 0);
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < compArr.Length; i++)
            {
                result.Append(compArr[i].ToString("X2"));
            }
            return(result.ToString());
        }
 public byte[] GetHash()
 {
     byte[] handshakeHash;
     if (_VerifyHandshake == null)
     {
         IDigest sha1 = new Org.BouncyCastle.Crypto.Digests.Sha1Digest(_VerifyHandshakeSHA1);
         IDigest md5  = new Org.BouncyCastle.Crypto.Digests.MD5Digest(_VerifyHandshakeMD5);
         handshakeHash = new byte[sha1.GetDigestSize() + md5.GetDigestSize()];
         md5.DoFinal(handshakeHash, 0);
         sha1.DoFinal(handshakeHash, md5.GetDigestSize());
     }
     else
     {
         IDigest hash = new Org.BouncyCastle.Crypto.Digests.Sha256Digest((Org.BouncyCastle.Crypto.Digests.Sha256Digest)_VerifyHandshake);
         handshakeHash = new byte[hash.GetDigestSize()];
         hash.DoFinal(handshakeHash, 0);
     }
     return(handshakeHash);
 }
        /// <summary>
        /// Retrieve the JWK Thumbprint of this key.
        /// </summary>
        /// <returns>A byte array of the calculated thumbprint digest.</returns>
        public byte[] Thumbprint()
        {
            if (KeyType == JWK.KeyType.RSA)
            {
                var hasher = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();

                var JWK = new Dictionary <string, string>
                {
                    { "e", Exponent },
                    { "kty", KeyType },
                    { "n", Modulus }
                };

                var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(JWK, Formatting.None));
                hasher.BlockUpdate(bytes, 0, bytes.Length);

                var digest = new byte[hasher.GetDigestSize()];
                hasher.DoFinal(digest, 0);

                return(digest);
            }

            throw new NotSupportedException("Unknown JWK key type.");
        }
Exemple #10
0
 public byte[] GetHash()
 {
     byte[] handshakeHash;
     if (_VerifyHandshake == null)
     {
         IDigest sha1 = new Org.BouncyCastle.Crypto.Digests.Sha1Digest(_VerifyHandshakeSHA1);
         IDigest md5 = new Org.BouncyCastle.Crypto.Digests.MD5Digest(_VerifyHandshakeMD5);
         handshakeHash = new byte[sha1.GetDigestSize() + md5.GetDigestSize()];
         md5.DoFinal(handshakeHash, 0);
         sha1.DoFinal(handshakeHash, md5.GetDigestSize());
     }
     else
     {
         IDigest hash = new Org.BouncyCastle.Crypto.Digests.Sha256Digest((Org.BouncyCastle.Crypto.Digests.Sha256Digest)_VerifyHandshake);
         handshakeHash = new byte[hash.GetDigestSize()];
         hash.DoFinal(handshakeHash, 0);
     }
     return handshakeHash;
 }