/// <summary> /// A method is used to validate the signed data by using specified public key. /// </summary> /// <param name="signedData">A parameter represents the signed data which will be validate.</param> /// <param name="originalData">A parameter represents the original data which is used to execute the validation.</param> /// <param name="publicKeyBlob">A parameter represents the binaries data of the public key part of a unique key-pairs.</param> /// <returns>Return 'true' indicating the signed data pass the validation.</returns> private static bool VerifySignedData(byte[] signedData, byte[] originalData, byte[] publicKeyBlob) { if (null == signedData || 0 == signedData.Length) { throw new ArgumentNullException("signedData"); } if (null == originalData || 0 == originalData.Length) { throw new ArgumentNullException("originalData"); } if (null == publicKeyBlob || 0 == publicKeyBlob.Length) { throw new ArgumentNullException("publicKeyBlob"); } using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider()) { rsaProvider.ImportCspBlob(publicKeyBlob); SHA256Managed sha = new SHA256Managed(); bool result = rsaProvider.VerifyData(originalData, sha, signedData); sha.Dispose(); return(result); } }
public static string ComputeHash(string text, string salt, HashAlgo hashType) { byte[] bytes = ASCIIEncoding.UTF8.GetBytes(text + salt); byte[] hash = null; switch (hashType) { case HashAlgo.SHA256: SHA256Managed sha256 = new SHA256Managed(); hash = sha256.ComputeHash(bytes); sha256.Dispose(); break; case HashAlgo.SHA384: SHA384Managed sha384 = new SHA384Managed(); hash = sha384.ComputeHash(bytes); sha384.Dispose(); break; case HashAlgo.SHA512: SHA512Managed sha512 = new SHA512Managed(); hash = sha512.ComputeHash(bytes); sha512.Dispose(); break; } return(Convert.ToBase64String(hash)); }
// theres two verify hash cuz i forgot which one can work or both cannot work. u just try!! public static string verifyyyByteHash(string password, byte[] saltBytes) { byte[] passwordTextBytes = Encoding.UTF8.GetBytes(password); //allocate array that will handle Password + salt size byte[] passwordTextBytesWithSalt = new byte[passwordTextBytes.Length + saltBytes.Length]; for (int i = 0; i < passwordTextBytes.Length; i++) { passwordTextBytesWithSalt[i] = passwordTextBytes[i]; } //append salt bytes to resulting array for (int i = 0; i < saltBytes.Length; i++) { passwordTextBytesWithSalt[passwordTextBytes.Length + i] = saltBytes[i]; } HashAlgorithm hash; hash = new SHA256Managed(); byte[] hashBytes = hash.ComputeHash(passwordTextBytesWithSalt); hash.Dispose(); byte[] hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length]; for (int i = 0; i < hashBytes.Length; i++) { hashWithSaltBytes[i] = hashBytes[i]; } for (int i = 0; i < saltBytes.Length; i++) { hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i]; } string hashValue = Convert.ToBase64String(hashWithSaltBytes); string salt = Encoding.UTF8.GetString(saltBytes); return(hashValue + "," + salt); }
private static string SHA256Text(Encoding encoding, string content) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException("content is null"); } string strRet = string.Empty; SHA256 sha = null; try { sha = new SHA256Managed(); byte[] buffContent = encoding.GetBytes(content); byte[] buff = sha.ComputeHash(buffContent); strRet = FormatByteArray(buff); } catch { throw; } finally { if (null != sha) { sha.Dispose(); sha = null; } } return(strRet); }
public static string SHA256(Stream stream) { if (null == stream) { throw new ArgumentNullException("stream is null"); } string strRet = string.Empty; SHA256 sha = null; try { sha = new SHA256Managed(); stream.Seek(0, SeekOrigin.Begin); byte[] buff = sha.ComputeHash(stream); strRet = FormatByteArray(buff); } catch { throw; } finally { if (null != sha) { sha.Dispose(); sha = null; } } return(strRet); }
public static string Encrypt(string password, string salt) { if (salt == null) { salt = ""; } byte[] plainData = ASCIIEncoding.UTF8.GetBytes(password); byte[] saltData = ASCIIEncoding.UTF8.GetBytes(salt); byte[] plainDataAndSalt = new byte[plainData.Length + salt.Length]; for (int x = 0; x < plainData.Length; x++) { plainDataAndSalt[x] = plainData[x]; } for (int n = 0; n < saltData.Length; n++) { plainDataAndSalt[plainData.Length + n] = saltData[n]; } byte[] hashValue = null; SHA256Managed sha = new SHA256Managed(); hashValue = sha.ComputeHash(plainDataAndSalt); sha.Dispose(); return(Convert.ToBase64String(hashValue)); }
public static String sha256(String s) { try { SHA256Managed crypt = new SHA256Managed(); Byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(s), 0, Encoding.UTF8.GetByteCount(s)); crypt.Clear(); crypt.Dispose(); StringBuilder hash = new StringBuilder(); foreach (Byte x in crypto) { hash.Append(x.ToString("x2")); } LastLogMsg = null; return(hash.ToString()); } catch (Exception ex) { #if DEBUG Console.WriteLine(ex.Message + " " + ex.StackTrace); #endif LastLogMsg = "Unhandled Exception!"; return(null); } }
public EncryptSettingsProvider() { //read settings from configuration var useHashingString = ConfigurationManager.AppSettings["UseHashingForEncryption"]; var useHashing = System.String.Compare(useHashingString, "false", System.StringComparison.OrdinalIgnoreCase) != 0; _encryptionPrefix = ConfigurationManager.AppSettings["EncryptionPrefix"]; if (string.IsNullOrWhiteSpace(_encryptionPrefix)) { _encryptionPrefix = "encryptedHidden_"; } var key = ConfigurationManager.AppSettings["EncryptionKey"]; if (useHashing) { var hash = new SHA256Managed(); _encryptionKey = hash.ComputeHash(Encoding.UTF8.GetBytes(key)); hash.Clear(); hash.Dispose(); } else { _encryptionKey = Encoding.UTF8.GetBytes(key); } }
public static string GetSha256String(byte[] buffer) { SHA256 sha512 = new SHA256Managed(); byte[] retVal = sha512.ComputeHash(buffer); //计算指定Stream 对象的哈希值 sha512.Dispose(); return(BitConverter.ToString(retVal).Replace("-", "")); }
private void Dispose(bool disposing) { _isDisposed = true; if (disposing) { _hasher.Dispose(); } }
public static string GenerateHash(string input, string salt) { byte[] bytes = Encoding.UTF8.GetBytes(input + salt); SHA256Managed sHA256ManagedString = new SHA256Managed(); byte[] hash = sHA256ManagedString.ComputeHash(bytes); sHA256ManagedString.Dispose(); return(Convert.ToBase64String(hash)); }
/// <summary> /// RSA签名 /// </summary> /// <param name="InOutParaType">输出的字符串类型</param> /// <param name="PrivateKey">私钥</param> /// <param name="HashbyteSignStr">待签名Hash字符串值</param> /// <param name="HashAlgorithmType">签名算法</param> /// <param name="Encode">字符串编码</param> public static string CreateRSASign(SafeHelper.EncryptionHelper.InOutParaType InOutParaType, string HashbyteSignStr, string PrivateKey, RSASignHashAlgorithmType HashAlgorithmType, Encoding Encode) { try { byte[] Buffer = Encode.GetBytes(HashbyteSignStr); byte[] HashbyteSign; switch (HashAlgorithmType) { case RSASignHashAlgorithmType.SHA1: SHA1Managed sha1 = new SHA1Managed(); HashbyteSign = sha1.ComputeHash(Buffer); sha1.Dispose(); break; case RSASignHashAlgorithmType.SHA256: SHA256Managed sha2 = new SHA256Managed(); HashbyteSign = sha2.ComputeHash(Buffer); sha2.Dispose(); break; default: HashAlgorithm MD5 = HashAlgorithm.Create("MD5"); HashbyteSign = MD5.ComputeHash(Buffer); MD5.Dispose(); break; } RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(PrivateKey); RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA); //设置签名的算法 RSAFormatter.SetHashAlgorithm(HashAlgorithmType.ToString()); //执行签名 byte[] RSASignbyte = RSAFormatter.CreateSignature(HashbyteSign); RSA.Dispose(); switch (InOutParaType) { case SafeHelper.EncryptionHelper.InOutParaType.Str16: return(BitConverter.ToString(RSASignbyte).Replace("-", string.Empty).ToUpper()); case SafeHelper.EncryptionHelper.InOutParaType.Base64: return(Convert.ToBase64String(RSASignbyte)); default: return(string.Empty); } } catch (Exception ex) { throw ex; } }
public string calculHash(string emailAddress) { byte[] plainData = ASCIIEncoding.UTF8.GetBytes(emailAddress); byte[] hashValue = null; SHA256Managed sha = new SHA256Managed(); hashValue = sha.ComputeHash(plainData); sha.Dispose(); return(Convert.ToBase64String(hashValue)); }
void SHA256() { SHA256 shaM = new SHA256Managed(); result = shaM.ComputeHash(data); ConsoleResult("SHA256"); shaM.Dispose(); }
protected override void Dispose(bool disposing) { System.Buffers.ArrayPool <byte> .Shared.Return(_Buffer); if (disposing) { sha.Dispose(); } base.Dispose(disposing); }
/// <summary> /// Encrypt a bit array. /// </summary> /// <param name="byteArray">Byte array to encrypt.</param> /// <returns>Bit array ciphered.</returns> public byte[] Encrypt(ref byte[] byteArray) { byte[] result = null; if (Type == CipherType.TripleDES) { if (IsTripleCapable()) { ICryptoTransform transform = null; try { transform = GetTripleDES().CreateEncryptor(); result = transform.TransformFinalBlock(byteArray, 0, byteArray.Length); } catch (Exception ex) { throw new PulsarException(typeof(Cipher).FullName, Pulsar.Toolkit.Resources.Exceptions.Cipher.TripleEncryptFailed, ex); } finally { if (transform != null) { transform.Dispose(); } } } else { ThrowTripleParamException(); } } else { SHA256Managed sha = null; try { sha = new SHA256Managed(); result = sha.ComputeHash(byteArray); } catch (Exception ex) { throw new PulsarException(typeof(Cipher).FullName, Pulsar.Toolkit.Resources.Exceptions.Cipher.SHA256EncryptFailed, ex); } finally { if (sha != null) { sha.Dispose(); } } } return(result); }
/* SHA256-hasing wordt hier gebruikt */ public static string ComputeHash(string plainText, byte[] salt) { const int minSaltLength = 4; const int maxSaltLength = 16; byte[] saltBytes = null; if (salt != null) { saltBytes = salt; } else { Random rand = new Random(); int saltLength = rand.Next(minSaltLength, maxSaltLength); saltBytes = new byte[saltLength]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetNonZeroBytes(saltBytes); rng.Dispose(); } byte[] plainData = ASCIIEncoding.UTF8.GetBytes(plainText); byte[] plainDataAndSalt = new byte[plainData.Length + saltBytes.Length]; for (int i = 0; i < plainData.Length; i++) { plainDataAndSalt[i] = plainData[i]; } for (int i = 0; i < saltBytes.Length; i++) { plainDataAndSalt[plainData.Length + i] = saltBytes[i]; } SHA256Managed sha = new SHA256Managed(); byte[] hashValue = sha.ComputeHash(plainDataAndSalt); sha.Dispose(); byte[] result = new byte[hashValue.Length + saltBytes.Length]; for (int i = 0; i < hashValue.Length; i++) { result[i] = hashValue[i]; } for (int i = 0; i < saltBytes.Length; i++) { result[hashValue.Length + i] = saltBytes[i]; } return(Convert.ToBase64String(result)); }
/// <summary> /// /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static string GetSha256String(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } SHA256 sha256 = new SHA256Managed(); byte[] retVal = sha256.ComputeHash(buffer); //计算指定Stream 对象的哈希值 sha256.Dispose(); return(BitConverter.ToString(retVal).Replace("-", "")); }
/// <summary> /// SHA256 Hash of the SSN, pass in the full 9 or the last 4 /// </summary> /// <param name="ssn"></param> /// <returns>Hashed version of SSN passed in</returns> public byte[] HashSSN(string ssn) { byte[] hashedSSN = null; SHA256 shaM = new SHA256Managed(); //Using UTF8 because this only contains ASCII text hashedSSN = shaM.ComputeHash(Encoding.UTF8.GetBytes(ssn)); shaM.Dispose(); return(hashedSSN); }
public static string ComputeHash(string plainText, byte[] saltBytes) { // Convert plain text into a byte array. byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); // Allocate array, which will hold plain text and salt. byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length]; // Copy plain text bytes into resulting array. for (int i = 0; i < plainTextBytes.Length; i++) { plainTextWithSaltBytes[i] = plainTextBytes[i]; } // Append salt bytes to the resulting array. for (int i = 0; i < saltBytes.Length; i++) { plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i]; } SHA256Managed hash = new SHA256Managed(); // Compute hash value of the plain text with appended salt. byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes); // Create array which will hold hash and original salt bytes. byte[] hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length]; // Copy hash bytes into resulting array. for (int i = 0; i < hashBytes.Length; i++) { hashWithSaltBytes[i] = hashBytes[i]; } // Append salt bytes to the result. for (int i = 0; i < saltBytes.Length; i++) { hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i]; } // Convert result into a base64-encoded string. string hashValue = Convert.ToBase64String(hashWithSaltBytes); hash.Dispose(); // Return the result. return(hashValue); }
public static string ComputeHash(string plainText, Supported_HA hash, byte[] salt) { int minSaltLength = 4, maxSaltLength = 16; byte[] saltBytes = null; if (salt != null) { saltBytes = salt; } else { Random r = new Random(); int SaltLength = r.Next(minSaltLength, maxSaltLength); saltBytes = new byte[SaltLength]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetNonZeroBytes(saltBytes); rng.Dispose(); } byte[] plainData = ASCIIEncoding.UTF8.GetBytes(plainText); byte[] plainDataWithSalt = new byte[plainData.Length + saltBytes.Length]; for (int x = 0; x < plainData.Length; x++) { plainDataWithSalt[x] = plainData[x]; } for (int n = 0; n < saltBytes.Length; n++) { plainDataWithSalt[plainData.Length + n] = saltBytes[n]; } byte[] hashValue = null; SHA256Managed sha = new SHA256Managed(); hashValue = sha.ComputeHash(plainDataWithSalt); sha.Dispose(); byte[] result = new byte[hashValue.Length + saltBytes.Length]; for (int x = 0; x < hashValue.Length; x++) { result[x] = hashValue[x]; } for (int n = 0; n < saltBytes.Length; n++) { result[hashValue.Length + n] = saltBytes[n]; } return(Convert.ToBase64String(result)); }
public string ComputeHash(string plainText, SupportedHash hash, byte[] salt) { byte[] plainData = Encoding.UTF8.GetBytes(plainText); byte[] plainDataWithSalt = new byte[plainData.Length + salt.Length]; for (int x = 0; x < plainData.Length; x++) { plainDataWithSalt[x] = plainData[x]; } for (int n = 0; n < salt.Length; n++) { plainDataWithSalt[plainData.Length + n] = salt[n]; } byte[] hashValue = null; switch (hash) { case SupportedHash.SHA256: SHA256Managed sha = new SHA256Managed(); hashValue = sha.ComputeHash(plainDataWithSalt); sha.Dispose(); break; case SupportedHash.SHA384: SHA384Managed sha1 = new SHA384Managed(); hashValue = sha1.ComputeHash(plainDataWithSalt); sha1.Dispose(); break; case SupportedHash.SHA512: SHA512Managed sha2 = new SHA512Managed(); hashValue = sha2.ComputeHash(plainDataWithSalt); sha2.Dispose(); break; } byte[] result = new byte[hashValue.Length + salt.Length]; for (int x = 0; x < hashValue.Length; x++) { result[x] = hashValue[x]; } for (int n = 0; n < salt.Length; n++) { result[hashValue.Length + n] = salt[n]; } return(Convert.ToBase64String(result)); }
/// <summary> /// /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static byte[] GetSha256Bytes(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } SHA256 sha256 = new SHA256Managed(); byte[] retVal = sha256.ComputeHash(buffer); //计算指定Stream 对象的哈希值 sha256.Dispose(); return(retVal); }
/// <summary> /// SHA256加密 哈希加密一个字符串 生成64位字符串 /// </summary> /// <param name="Security"></param> /// <returns></returns> public static string SHA256String(string encodingString) { byte[] msg = Encoding.UTF8.GetBytes(encodingString); SHA256Managed Arithmetic = new SHA256Managed(); byte[] Value = Arithmetic.ComputeHash(msg); StringBuilder sb = new StringBuilder(); foreach (byte o in Value) { sb.Append(o.ToString("X2")); } Arithmetic.Dispose(); return(sb.ToString()); }
private string GetHash(string data) { var message = Encoding.ASCII.GetBytes(data); SHA256Managed hashString = new SHA256Managed(); string hex = ""; var hashValue = hashString.ComputeHash(message); foreach (byte x in hashValue) { hex += string.Format("{0:x2}", x); } hashString.Dispose(); return(hex); }
/// <summary> /// Signs data using the provided private key. /// </summary> /// <param name="dataToSign">The data to be signed.</param> /// <param name="privateKey">The private key.</param> /// <returns>The signature of the data as signed by the private key.</returns> public EncryptionData Sign(EncryptionData dataToSign, RsaPrivateKey privateKey) { var rsa = GetRsaProvider(); rsa.ImportParameters(Check.NotNull(privateKey, nameof(privateKey)).ToParameters()); var hash = new SHA256Managed(); var sig = rsa.SignData(Check.NotNull(dataToSign, nameof(dataToSign)).Bytes, hash); rsa.Clear(); rsa.Dispose(); hash.Dispose(); return(new EncryptionData(sig)); }
/// <summary> /// Retorna um hash a partir dos dados fornecidos. /// </summary> /// <param name="data">dados a serem computados</param> /// <returns></returns> public string Compute(string data) { var sha = new SHA256Managed(); var hash = new StringBuilder(); byte[] buffer = sha.ComputeHash(Encoding.ASCII.GetBytes(data)); foreach (var bytes in buffer) { hash.Append(bytes.ToString("x2")); } sha.Dispose(); return(hash.ToString()); }
public string GetPasswordHashValue(string plainPassword) { byte[] password = Encoding.UTF8.GetBytes(plainPassword); SHA256Managed sha = new SHA256Managed(); byte[] hashedPasword = sha.ComputeHash(password); sha.Dispose(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < hashedPasword.Length; i++) { stringBuilder.Append(hashedPasword[i].ToString("x2")); } return(stringBuilder.ToString()); }
public static string ComputePasswordHash(string plainText) { // Convert plain text into a byte array. byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); SHA256Managed hash = new SHA256Managed(); // Compute hash value of the plain text with appended salt. byte[] hashBytes = hash.ComputeHash(plainTextBytes); // Convert result into a base64-encoded string. string hashValue = Convert.ToBase64String(hashBytes); hash.Dispose(); return(hashValue); }
/// <summary> /// Compute Hash using SHA-256 and a random salt of length 15 bytes /// </summary> /// <param name="text">Text to be hashed</param> /// <param name="salt">Optional salt to be used for the hashing</param> /// <returns>array of bytes, the hash with the salt appended to it</returns> public static byte[] ComputeHash(string text, byte[] salt = null) { byte[] saltBytes = null; if (salt != null) { saltBytes = salt; } else { int SaltLength = 15; saltBytes = new byte[SaltLength]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetNonZeroBytes(saltBytes); rng.Dispose(); } byte[] plainData = Encoding.UTF8.GetBytes(text); byte[] plainDataWithSalt = new byte[plainData.Length + saltBytes.Length]; for (int x = 0; x < plainData.Length; x++) { plainDataWithSalt[x] = plainData[x]; } for (int n = 0; n < saltBytes.Length; n++) { plainDataWithSalt[plainData.Length + n] = saltBytes[n]; } byte[] hashValue = null; SHA256Managed sha = new SHA256Managed(); hashValue = sha.ComputeHash(plainDataWithSalt); sha.Dispose(); byte[] result = new byte[hashValue.Length + saltBytes.Length]; for (int x = 0; x < hashValue.Length; x++) { result[x] = hashValue[x]; } for (int n = 0; n < saltBytes.Length; n++) { result[hashValue.Length + n] = saltBytes[n]; } return(result); }