/// <summary> /// Given a file path, compute the file hashes. /// </summary> /// <param name="filePath"></param> /// <param name="md5Hash"></param> /// <param name="sha1Hash"></param> /// <param name="sha256Hash"></param> public static void ComputeHashes(string filePath, out byte[] md5Hash, out byte[] sha1Hash, out byte[] sha256Hash) { using (var md5 = MD5Cng.Create()) using (var sha1 = SHA1Cng.Create()) using (var sha256 = SHA256Cng.Create()) using (var input = File.OpenRead(filePath)) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) { md5.TransformBlock(buffer, 0, bytesRead, buffer, 0); sha1.TransformBlock(buffer, 0, bytesRead, buffer, 0); sha256.TransformBlock(buffer, 0, bytesRead, buffer, 0); } // We have to call TransformFinalBlock, but we don't have any // more data - just provide 0 bytes. md5.TransformFinalBlock(buffer, 0, 0); sha1.TransformFinalBlock(buffer, 0, 0); sha256.TransformFinalBlock(buffer, 0, 0); md5Hash = md5.Hash; sha1Hash = sha1.Hash; sha256Hash = sha256.Hash; } }
//============================================================ // STATIC METHODS //============================================================ #region GenerateHash(Stream stream, YahooMediaHashAlgorithm algorithm) /// <summary> /// Computes the hash value for the supplied <see cref="Stream"/> using the specified <see cref="YahooMediaHashAlgorithm"/>. /// </summary> /// <param name="stream">The input to compute the hash code for.</param> /// <param name="algorithm">A <see cref="YahooMediaHashAlgorithm"/> enumeration value that indicates the algorithm to use.</param> /// <returns>The <b>base64</b> encoded result of the computed hash code.</returns> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentException">The <paramref name="algorithm"/> is equal to <see cref="YahooMediaHashAlgorithm.None"/>.</exception> public static string GenerateHash(Stream stream, YahooMediaHashAlgorithm algorithm) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ string base64EncodedHash = String.Empty; MD5 md5 = MD5Cng.Create(); SHA1 sha1 = SHA1Cng.Create(); //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(stream, "stream"); if (algorithm == YahooMediaHashAlgorithm.None) { throw new ArgumentException(String.Format(null, "Unable to generate a hash value for the {0} algorithm.", algorithm), "algorithm"); } if (algorithm == YahooMediaHashAlgorithm.MD5) { byte[] hash = md5.ComputeHash(stream); base64EncodedHash = Convert.ToBase64String(hash); } else if (algorithm == YahooMediaHashAlgorithm.Sha1) { byte[] hash = sha1.ComputeHash(stream); base64EncodedHash = Convert.ToBase64String(hash); } return(base64EncodedHash); }
public static byte[] ToMD5(this byte[] s) { using (var md5 = new MD5Cng()) { return(md5.ComputeHash(s)); } }
private static byte[] ComputeHash(string hashAlgorithmName, string fileName) { HashAlgorithm hashAlgorithm = null; switch (hashAlgorithmName) { case StrMd5: hashAlgorithm = new MD5Cng(); break; case StrSha1: hashAlgorithm = new SHA1Cng(); break; case StrSha256: hashAlgorithm = new SHA256Cng(); break; } if (null != hashAlgorithm) { using (var stream = File.OpenRead(fileName)) { return(hashAlgorithm.ComputeHash(stream)); } } var message = String.Format("Invalid hash algorithm name: {0}", hashAlgorithmName); throw new ApplicationException(message); }
public static string Hash(byte[] data) { using (var md5 = MD5Cng.Create()) { return(BitConverter.ToString(md5.ComputeHash(data)).Replace("-", string.Empty)); } }
public Respone Login(LoginRequest request) { var res = DBServer.Instance.GetPassword(request.Account); if (res.IsSuccess) { var oldPass = res.Data.Rows[0]["password"].ToString(); var salt = Convert.FromBase64String(res.Data.Rows[0]["salt"].ToString()); var saltpass = Encoding.UTF8.GetBytes(request.Password).Concat(salt).ToArray(); MD5Cng md5 = new MD5Cng(); var crpPass = Convert.ToBase64String(md5.ComputeHash(saltpass)); if (oldPass == crpPass) { Context.Login(request.Account); return(StandResult(StandRespone.SuccessResult("登录成功"))); } else { return(StandResult(StandRespone.FailResult("登录失败,密码错误"))); } } else { return(StandResult(res)); } }
/// <summary> /// MD5加密 /// </summary> /// <param name="password"></param> /// <param name="passwordFormat"></param> /// <returns></returns> public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat) { HashAlgorithm algorithm; if (password == null) { throw new ArgumentNullException("password"); } if (passwordFormat == null) { throw new ArgumentNullException("passwordFormat"); } if (EqualsIgnoreCase(passwordFormat, "sha1")) { algorithm = new SHA1Cng(); } else if (EqualsIgnoreCase(passwordFormat, "md5")) { algorithm = new MD5Cng(); } else { object[] args = new object[] { "passwordFormat" }; throw new ArgumentException("InvalidArgumentValue"); } using (algorithm) { return(BinaryToHex(algorithm.ComputeHash(Encoding.UTF8.GetBytes(password)))); } }
static byte[] GetHash(string value) { using (MD5 hashAlgorithm = new MD5Cng()) { return(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(value))); } }
static void day4(int part = 1) { decimal i = 0; MD5Cng md5 = new MD5Cng(); md5.Initialize(); string hash; while (true) { hash = ByteArrayToString(md5.ComputeHash(Encoding.ASCII.GetBytes("iwrupvqb" + i.ToString()))); if (part == 1) { if (hash.StartsWith("00000")) { break; } } else { if (hash.StartsWith("000000")) { break; } } ++i; } System.Console.WriteLine(hash + " | " + i); }
protected Guid GetUserHash(string userName) { MD5Cng md5 = new MD5Cng(); var bytes = md5.ComputeHash(Encoding.ASCII.GetBytes(userName)); return(new Guid(bytes)); }
public static void ComputeHashes(string fileName, out string md5, out string sha1, out string sha256) { sha1 = sha256 = md5 = null; try { using (FileStream stream = File.OpenRead(fileName)) { using (var bufferedStream = new BufferedStream(stream, 1024 * 32)) { var md5Cng = new MD5Cng(); byte[] checksum = md5Cng.ComputeHash(bufferedStream); md5 = BitConverter.ToString(checksum).Replace("-", String.Empty); stream.Seek(0, SeekOrigin.Begin); bufferedStream.Seek(0, SeekOrigin.Begin); var sha1Cng = new SHA1Cng(); checksum = sha1Cng.ComputeHash(bufferedStream); sha1 = BitConverter.ToString(checksum).Replace("-", String.Empty); stream.Seek(0, SeekOrigin.Begin); bufferedStream.Seek(0, SeekOrigin.Begin); var sha256Cng = new SHA256Cng(); checksum = sha256Cng.ComputeHash(bufferedStream); sha256 = BitConverter.ToString(checksum).Replace("-", String.Empty); } } } catch (IOException) { } catch (UnauthorizedAccessException) { } }
/// <summary> /// 对密码做 Hash运算 /// </summary> /// <param name="password">密码</param> /// <param name="hashFromat">哈希算法</param> /// <returns></returns> private static string HashPassword(string password, HashFromat hashFromat) { if (password == null) { throw new ArgumentNullException(nameof(password)); } if (password == null) { throw new ArgumentNullException("密码不能为空"); } HashAlgorithm hashAlgorithm = new MD5Cng(); switch (hashFromat) { case HashFromat.Md5: hashAlgorithm = new MD5Cng(); break; case HashFromat.Sha1: hashAlgorithm = new SHA1Cng(); break; } using (hashAlgorithm) { return(BinaryToHex(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(password)))); } }
/// <summary> /// md5 hash /// </summary> public static string MD5(string input, int iterations, bool lowerCase = true) { if (string.IsNullOrEmpty(input)) { return(string.Empty); } if (iterations < 1) { iterations = 1; } var bytes1 = Encoding.UTF8.GetBytes(input); var bytes2 = new byte[] { 9, 81 }; using (var hashAlgorithm = new MD5Cng()) { for (var i = 0; i < iterations; i++) { var bytes = new byte[bytes1.Length + bytes2.Length]; bytes1.CopyTo(bytes, 0); bytes2.CopyTo(bytes, bytes1.Length); bytes2 = hashAlgorithm.ComputeHash(bytes); } var hexString = BinaryUtil.ToHex(bytes2); return(lowerCase ? hexString.ToLowerInvariant() : hexString); } }
/// <summary> /// Create a new instance of the <see cref="EncryptionManager"/> class /// </summary> /// <param name="encryptionCertificate"> /// The certificate that contains the secrets used for encryption. Note that /// the private key must be present in this certificate in order for decryption /// to be performed. /// </param> /// <param name="mode">The mode (encryption/decryption) of the encryption manager</param> /// <param name="outputStream">The stream where the transformed content will be written</param> /// <param name="sourceFileSize"></param> public EncryptionManager( X509Certificate2 encryptionCertificate, EncryptionMode mode, Stream outputStream, long sourceFileSize) { Pre.ThrowIfArgumentNull(encryptionCertificate, nameof(encryptionCertificate)); Pre.ThrowIfArgumentNull(outputStream, nameof(outputStream)); Pre.ThrowIfTrue(mode == EncryptionMode.None, "Encryption mode cannot be None"); this.encryptionCertificate = encryptionCertificate; this.Mode = mode; this.sourceFileSize = sourceFileSize; this.outputStream = outputStream; this.sha1 = new SHA1Cng(); this.md5 = new MD5Cng(); // Any valid encrypted file will have a minimum size (to include the header and minimal // encrypted content). Ensure that the source file is at least this size. if (mode == EncryptionMode.Decrypt) { Pre.Assert(sourceFileSize >= MinimumEncryptedFileSize, "sourceFileSize >= minimumEncryptedFileSize"); } this.Initialize(); }
private static string GetHash(string s) { MD5 sec = new MD5Cng(); ASCIIEncoding enc = new ASCIIEncoding(); byte[] bt = enc.GetBytes(s); return(GetHexString(sec.ComputeHash(bt))); }
public byte[] MD5(string pwd) { byte[] encryptPWD = new ASCIIEncoding().GetBytes(pwd); //调用MD5类加密数据 MD5Cng m = new MD5Cng(); return(m.ComputeHash(encryptPWD)); }
public static Guid ComputeGuidMD5(byte[] bytes) { using (var hashAlgorithmImpl = new MD5Cng()) { var hashBytes = hashAlgorithmImpl.ComputeHash(bytes); return(new Guid(hashBytes)); } }
public static String ComputeMD5(byte[] bytes) { using (var hashAlgorithmImpl = new MD5Cng()) { var hashBytes = hashAlgorithmImpl.ComputeHash(bytes); return(String.Concat(hashBytes.Select(b => b.ToString("x2")))); } }
public static string Md5Converter(string str) { var md5 = new MD5Cng(); var data = md5.ComputeHash(Encoding.Default.GetBytes(str)); var pwdmd5 = data.Aggregate(string.Empty, (current, t) => current + t.ToString("x2")); return(pwdmd5); }
private string GetETag(Stream stream) { using (var md5 = new MD5Cng()) { md5.ComputeHash(stream); return(Convert.ToBase64String(md5.Hash)); } }
public static byte[] Md5EncodeByte(string text) { var md5 = new MD5Cng(); var oldbytes = Encoding.UTF8.GetBytes(text); return(md5.ComputeHash(Encoding.UTF8.GetBytes(text))); }
public byte[] CalculateHash(string filePath) { using (var file = File.OpenRead(filePath)) using (var md5 = new MD5Cng()) { return(md5.ComputeHash(file)); } }
// 获得Md5字符串 public static string GetMD5String(string str) { using (MD5 md5 = new MD5Cng()) { byte[] values = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); return(BitConverter.ToString(values).Replace("-", "")); } }
//[Obsolete("The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.")] #region 加密方法 public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat) { string result = ""; if (password == null) { throw new ArgumentNullException("password"); } if (passwordFormat == null) { throw new ArgumentNullException("passwordFormat"); } HashAlgorithm hashAlgorithm; switch (passwordFormat) { case "sha1": hashAlgorithm = new SHA1Cng(); break; case "md5": hashAlgorithm = new MD5Cng(); break; case "sha256": hashAlgorithm = new HMACSHA256(); break; case "sha384": hashAlgorithm = new HMACSHA384(); break; case "sha512": hashAlgorithm = new HMACSHA512(); break; default: throw new ArgumentException("加解密类型不存在"); } using (hashAlgorithm) { byte[] bt = Encoding.UTF8.GetBytes(password); byte[] btp = hashAlgorithm.ComputeHash(bt); foreach (byte num in btp) { if (num < 0x10) { result = result + "0" + num.ToString("X"); } else { result = result + num.ToString("X"); } } } return(result); }
private string FileChecksum(string fileName) { using (var stream = new BufferedStream(File.OpenRead(fileName), 1200000)) { MD5Cng md5 = new MD5Cng(); byte[] checksum = md5.ComputeHash(stream); return(BitConverter.ToString(checksum).Replace("-", string.Empty)); } }
public static string Encrypt(string value) { HashAlgorithm algorithm = new MD5Cng(); using (algorithm) { return(BinaryToHex(algorithm.ComputeHash(Encoding.UTF8.GetBytes(value)))); } }
/// <summary> /// CheckSum128 method implementation /// </summary> public static byte[] CheckSum128(byte[] value) { byte[] hash = null; using (MD5 md5 = MD5Cng.Create()) { hash = md5.ComputeHash(value); } return(hash); }
public static string MD5Hash(string str) { using (var md5 = MD5Cng.Create()) { var result = md5.ComputeHash(Encoding.ASCII.GetBytes(str)); var strResult = BitConverter.ToString(result); return(strResult.Replace("-", "")); } }
/** * Encrypt user password before storing it in the database */ public static String EncryptPassword(TextBox txtPass) { // Encrypt password Start MD5Cng hashEnc = new MD5Cng(); byte[] password = Encoding.ASCII.GetBytes(txtPass.Text); byte[] hashedPass = hashEnc.ComputeHash(password); return(Encoding.ASCII.GetString(hashedPass)); }
public static string MD5Encode(string encodeable) { UnicodeEncoding uEncode = new UnicodeEncoding(); byte[] bytD2e = uEncode.GetBytes(encodeable); MD5Cng sha = new MD5Cng(); byte[] hash = sha.ComputeHash(bytD2e); return(Convert.ToBase64String(hash)); }