예제 #1
0
        public static string SHA512Hash(byte[] input)
        {
            var sha    = SHA256CryptoServiceProvider.Create();
            var hashed = sha.ComputeHash(input);

            return(BitConverter.ToString(hashed));
        }
예제 #2
0
        public static string SimpleEncrypt(string password)
        {
            byte[]        clear = Encoding.ASCII.GetBytes(password);
            HashAlgorithm sha2  = SHA256CryptoServiceProvider.Create();

            byte[] hashed = sha2.ComputeHash(clear);
            return(Convert.ToBase64String(hashed));
        }
예제 #3
0
    public string getSHA256Hash(string tInput)
    {
        byte[] input = Encoding.UTF8.GetBytes(tInput);
        SHA256 hash  = SHA256CryptoServiceProvider.Create();

        //Makes letters lowercase and removes hyphens to standardize the look of the hash, and make it URI friendly
        return(BitConverter.ToString(hash.ComputeHash(input)).ToLower().Replace("-", ""));
    }
예제 #4
0
 public string ComputeFileHash(string fileName)
 {
     using (var fileStream = File.OpenRead(fileName))
     {
         var hash       = (SHA256CryptoServiceProvider.Create()).ComputeHash(fileStream);
         var hashString = Convert.ToBase64String(hash);
         return(hashString);
     }
 }
예제 #5
0
 /// <summary>
 /// Calculates SHA256 HASH
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public static byte[] ComputeHash(byte[] input)
 {
     //using (SHA512 hashAlgorithm = SHA512CryptoServiceProvider.Create())
     using (SHA256 hashAlgorithm = SHA256CryptoServiceProvider.Create())
     {
         byte[] hash = hashAlgorithm.ComputeHash(input);
         return(hash);
     }
 }
예제 #6
0
 private static string ComputeSignature(string data, string key)
 {
     using (var sha = SHA256CryptoServiceProvider.Create())
     {
         sha.Initialize();
         var bytes = System.Text.Encoding.UTF8.GetBytes(data + key);
         return(Convert.ToBase64String(sha.ComputeHash(bytes)));
     }
 }
예제 #7
0
 static BlueConfiguration()
 {
     using (SHA256 sha = SHA256CryptoServiceProvider.Create())
         using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
         {
             PrivateKey = rsa.ToXmlString(true);
             PublicKey  = rsa.ToXmlString(false);
         }
 }
예제 #8
0
 public bool IsValidLicense()
 {
     using (SHA256 sha = SHA256CryptoServiceProvider.Create())
         using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
         {
             byte[] signature = Convert.FromBase64String(Key);
             rsa.FromXmlString(BlueConfiguration.PublicKey);
             return(rsa.VerifyData(User.GetSignature(), sha, signature));
         }
 }
예제 #9
0
        public string ComputeMd5(string value)
        {
            // FIPS-compliant MD5
            var md5     = SHA256CryptoServiceProvider.Create();
            var bytes   = Encoding.UTF8.GetBytes(value);
            var hash    = md5.ComputeHash(bytes);
            var hashStr = HexUtil.ByteArrayToHex(hash);

            return(hashStr);
        }
예제 #10
0
        /// <summary>
        /// Gets the alogrithm.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="hashName">Name of the hash algorithm.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">
        /// </exception>
        private static HashAlgorithm GetHashAlogrithm(HashMethod method, string hashName = null)
        {
            if (hashName == null)
            {
                switch (method)
                {
                case HashMethod.MD5:
                    return(MD5CryptoServiceProvider.Create());

                case HashMethod.SHA1:
                    return(SHA1CryptoServiceProvider.Create());

                case HashMethod.SHA256:
                    return(SHA256CryptoServiceProvider.Create());

                case HashMethod.SHA384:
                    return(SHA384CryptoServiceProvider.Create());

                case HashMethod.SHA512:
                    return(SHA512CryptoServiceProvider.Create());

                case HashMethod.CRC32:
                    return(new CRC32());

                default:
                    throw new NotSupportedException(
                              string.Format("Method [{0}] is not supported.", method.ToString()));
                }
            }
            else
            {
                switch (method)
                {
                case HashMethod.MD5:
                    return(MD5CryptoServiceProvider.Create(hashName));

                case HashMethod.SHA1:
                    return(SHA1CryptoServiceProvider.Create(hashName));

                case HashMethod.SHA256:
                    return(SHA256CryptoServiceProvider.Create(hashName));

                case HashMethod.SHA384:
                    return(SHA384CryptoServiceProvider.Create(hashName));

                case HashMethod.SHA512:
                    return(SHA512CryptoServiceProvider.Create(hashName));

                default:
                    throw new NotSupportedException(
                              string.Format("Method [{0}] is not supported.", method.ToString()));
                }
            }
        }
예제 #11
0
        /// <summary>
        /// Returns 44 character length hash of provided string
        /// </summary>
        /// <param name="clearText">The string to hash</param>
        /// <returns>SHA256 hash of clearText value</returns>
        public string CreateHash(string clearText)
        {
            if (String.IsNullOrEmpty(clearText))
            {
                return(clearText);
            }

            using (SHA256 sha = SHA256CryptoServiceProvider.Create())
            {
                var hashed = sha.ComputeHash(Encoding.UTF8.GetBytes(clearText));
                return(Convert.ToBase64String(hashed));
            }
        }
예제 #12
0
 public static byte[] ComputeHash(byte[] input, uint level)
 {
     byte[] buffer = input;
     for (int i = 0; i < level; i++)
     {
         //using (SHA512 hashAlgorithm = SHA512CryptoServiceProvider.Create())
         using (SHA256 hashAlgorithm = SHA256CryptoServiceProvider.Create())
         {
             buffer = hashAlgorithm.ComputeHash(buffer);
         }
     }
     return(buffer);
 }
 public Boolean VerifySignature(Byte[] iData,
                                Byte[] iSignature)
 {
     using (SHA256 pSHAHash = SHA256CryptoServiceProvider.Create())
     {
         Byte[] pBytSHAHash = pSHAHash.ComputeHash(iData);
         using (RSACryptoServiceProvider pCSPRSA = (RSACryptoServiceProvider)RSACryptoServiceProvider.Create())
         {
             pCSPRSA.ImportParameters(RSAParameters);
             return(pCSPRSA.VerifyHash(pBytSHAHash, CryptoConfig.MapNameToOID("SHA256"), iSignature));
         }
     }
 }
 public String Sign(Byte[] iData)
 {
     using (SHA256 pSHAHash = SHA256CryptoServiceProvider.Create())
     {
         Byte[] pBytSHAHash = pSHAHash.ComputeHash(iData);
         using (RSACryptoServiceProvider pCSPRSA = (RSACryptoServiceProvider)RSACryptoServiceProvider.Create())
         {
             pCSPRSA.ImportParameters(RSAParameters);
             Byte[] pBytSignature = pCSPRSA.SignHash(pBytSHAHash, CryptoConfig.MapNameToOID("SHA256"));
             return(Convert.ToBase64String(pBytSignature));
         }
     }
 }
예제 #15
0
        /// <summary>
        /// Generate a license for the particular user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static License GenerateLicense(User user)
        {
            License license;

            using (SHA256 sha = SHA256CryptoServiceProvider.Create())
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                {
                    rsa.FromXmlString(BlueConfiguration.PrivateKey);
                    byte[] hash       = rsa.SignData(user.GetSignature(), sha);
                    string licenseKey = Convert.ToBase64String(hash);
                    license = License.Create(user, licenseKey);
                }
            return(license);
        }
예제 #16
0
        public string encriptar_SHA256(string texto)
        {
            SHA256 sha256 = SHA256CryptoServiceProvider.Create();

            byte[]        textOriginal = ASCIIEncoding.Default.GetBytes(texto);
            byte[]        hash         = sha256.ComputeHash(textOriginal);
            StringBuilder cadena       = new StringBuilder();

            foreach (byte i in hash)
            {
                cadena.AppendFormat("{0:x2}", i);
            }
            return(cadena.ToString());
        }
예제 #17
0
        /// <summary>
        /// Calculates SHA256 HASH
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static byte[] ComputeHash(Memory <byte> input)
        {
            if (MemoryMarshal.TryGetArray(input, out ArraySegment <byte> byteArray))
            {
                //using (SHA512 hashAlgorithm = SHA512CryptoServiceProvider.Create())
                using (SHA256 hashAlgorithm = SHA256CryptoServiceProvider.Create())
                {
                    byte[] hash = hashAlgorithm.ComputeHash(byteArray.Array);
                    return(hash);
                }
            }

            throw new FailedToMarshalToByteArrayException(nameof(input));
        }
예제 #18
0
        /// <summary>
        /// This private method generates the hash code of the user pwd
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        private static string HashPwd(string Input)
        {
            byte[]        plain = Encoding.ASCII.GetBytes(Input);       // Get plain user pwd and converts t byte array
            HashAlgorithm sha2  = SHA256CryptoServiceProvider.Create(); //Create a object HashAlgorithm

            byte[] hashed = sha2.ComputeHash(plain);                    // Hash the plain pwd
            string hash   = "";

            foreach (byte b in hashed)
            {
                hash = hash + b.ToString("X2"); // get for every hash bytes its hexadecimal representation
            }
            return(hash);
        }
예제 #19
0
 public static string GetSHA256Hash(string s)
 {
     if (string.IsNullOrEmpty(s))
     {
         throw new ArgumentException("An empty string value cannot be hashed.");
     }
     Byte[] data = System.Text.Encoding.UTF8.GetBytes(s);
     Byte[] hash;
     using (var sha = SHA256CryptoServiceProvider.Create())
     {
         hash = sha.ComputeHash(data);
     }
     return(Convert.ToBase64String(hash));
 }
예제 #20
0
        private string GetStringHash(string text)
        {
            SHA256 sha256 = SHA256CryptoServiceProvider.Create();

            byte[] inputBytes = Encoding.ASCII.GetBytes(text);
            byte[] hash       = sha256.ComputeHash(inputBytes);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return(sb.ToString());
        }
예제 #21
0
        private string ComputeSHA256Hash(string uniqueId, string authenticationMetadataUrl, byte[] salt)
        {
            byte[] inputBytes = Encoding.ASCII.GetBytes(string.Concat(uniqueId, authenticationMetadataUrl));

            // Combine input bytes and salt.
            byte[] saltedInput = new byte[salt.Length + inputBytes.Length];
            salt.CopyTo(saltedInput, 0);
            inputBytes.CopyTo(saltedInput, salt.Length);

            // Compute the unique key.
            byte[] hashedBytes = SHA256CryptoServiceProvider.Create().ComputeHash(saltedInput);

            // Convert the hashed value to a string and return.
            return(BitConverter.ToString(hashedBytes));
        }
예제 #22
0
        private string ComputeUniqueIdentification()
        {
            byte[] inputBytes = Encoding.ASCII.GetBytes(string.Concat(msexchuid, amurl));

            // Combine input bytes and salt.
            byte[] saltedInput = new byte[Salt.Length + inputBytes.Length];
            Salt.CopyTo(saltedInput, 0);
            inputBytes.CopyTo(saltedInput, Salt.Length);

            // Compute the unique key.
            byte[] hashedBytes = SHA256CryptoServiceProvider.Create().ComputeHash(saltedInput);

            // Convert the hashed value to a string and return.
            return(BitConverter.ToString(hashedBytes));
        }
예제 #23
0
        /*
         * Calcule le hash sha256 d'un fichier dont on précise le nom
         * Susceptible de lever une exception si le fichier ou le réperroire n'existe pas
         * */
        public static String computeSha256Hash(String filename)
        {
            SHA256 sha256 = SHA256CryptoServiceProvider.Create();

            // Create a fileStream for the file.
            byte[]     hashValue;
            FileStream fileStream = new FileStream(filename, FileMode.Open);

            // Be sure it's positioned to the beginning of the stream.
            fileStream.Position = 0;
            // Compute the hash of the fileStream.
            hashValue = sha256.ComputeHash(fileStream);
            String dataSha256 = Utils.getReadableByteArray(hashValue).ToLower();

            fileStream.Close();
            return(dataSha256);
        }
예제 #24
0
        /// <summary>Constructor</summary>
        /// <param name="param">ECParameters</param>
        /// <param name="isPrivate">bool</param>
        public JWS_ES256_Param(ECParameters param, bool isPrivate)
        {
            // Cng or OpenSsl
            if (os.Platform == PlatformID.Win32NT)
            {
                this.DigitalSignECDsaCng = new DigitalSignECDsaCng(param, isPrivate);
            }
            else
            {
#if NETSTD
                this.DigitalSignECDsaOpenSsl = new DigitalSignECDsaOpenSsl(
                    param, SHA256CryptoServiceProvider.Create());
                //HashAlgorithmCmnFunc.CreateHashAlgorithmSP(EnumHashAlgorithm.SHA256_M));
#else
                throw new NotImplementedException(PublicExceptionMessage.NOT_IMPLEMENTED);
#endif
            }
        }
예제 #25
0
        public static String ApplySHA256(String input)
        {
            try
            {
                byte[]        hash     = SHA256CryptoServiceProvider.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < hash.Length; i++)
                {
                    sBuilder.Append(hash[i].ToString("x2"));
                }
                return(sBuilder.ToString());
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);

                return(null);
            }
        }
예제 #26
0
        public static string Encrypt(String srcStr)
        {
            String desStr = "";

            byte[] buf = Encoding.UTF8.GetBytes(srcStr);
            try
            {
                SHA256 sha = SHA256CryptoServiceProvider.Create();
                using (MemoryStream ms = new MemoryStream(buf))
                {
                    byte[] digest = sha.ComputeHash(ms);
                    desStr = ByteArray2HexString(digest);
                }
            }
            catch (System.Security.Cryptography.CryptographicException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
            return(desStr);
        }
예제 #27
0
        public SHA SHA256(string input, bool igonreCase)
        {
            _input      = input;
            _igonreCase = igonreCase;

            if (string.IsNullOrEmpty(input))
            {
                input = string.Empty;
            }

            if (igonreCase)
            {
                input = input.ToUpper();
            }

            try
            {
                _sha = SHA256CryptoServiceProvider.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
            }
            catch { }

            return(this);
        }
예제 #28
0
        public string CalculatePasswordHash(string userName, int userID, string password)
        {
            SHA512 hashProvider512 = SHA512CryptoServiceProvider.Create();
            SHA256 hashProvider256 = SHA256CryptoServiceProvider.Create();

            string salt = userID.ToString() + userName;                                                        // construct the salt from the useriD + username

            byte[] saltBytes = new byte[salt.Length * sizeof(char)];                                           // initialise new bytearray, size of the string times size of each char
            System.Buffer.BlockCopy(salt.ToCharArray(), 0, saltBytes, 0, salt.Length);                         // don't use encoding, just copy pure byte data

            byte[] saltHash       = hashProvider256.ComputeHash(saltBytes);                                    // compute the hash from the salt string
            string saltString     = Convert.ToBase64String(saltHash);                                          // convert the hash back to a string
            string passwordString = password + saltString;                                                     // construct the complete password from the password and the saltstring

            byte[] passwordBytes = new byte[passwordString.Length * sizeof(char)];                             // and initialise the new bytearray
            System.Buffer.BlockCopy(passwordString.ToCharArray(), 0, passwordBytes, 0, passwordString.Length); // copy it over


            byte[] passwordHash       = hashProvider512.ComputeHash(passwordBytes); // generate the final hash
            string passwordHashString = Convert.ToBase64String(passwordHash);       // and transform that back into a stringr

            return(passwordHashString);
        }
예제 #29
0
파일: SHA256.cs 프로젝트: liulilittle/nsjs
 protected override global::System.Security.Cryptography.HashAlgorithm New()
 {
     return(SHA256CryptoServiceProvider.Create());
 }
예제 #30
0
파일: SHA256.cs 프로젝트: Shawak/shawlib
 public static string Hash(Stream stream)
 {
     using (var sha256 = SHA256CryptoServiceProvider.Create())
         return(BitConverter.ToString(sha256.ComputeHash(stream)).Replace("-", "").ToLower());
 }