public void DecryptColosseum(byte[] data, byte[] digest) { byte[] key = new byte[20]; byte[] k = new byte[20]; byte[] d = new byte[20]; ByteHelper.ReplaceBytes(key, 0, digest); ByteHelper.ReplaceBytes(k, 0, key); for (int i = 0; i < 20; i++) { k[i] = (byte)~k[i]; } ByteHelper.ReplaceBytes(key, 0, k); SHA1 sha1 = SHA1Cng.Create(); for (int i = 0x18; i < 0x1DFD8; i += 20) { key = sha1.ComputeHash(data, i, 20); ByteHelper.ReplaceBytes(d, 0, ByteHelper.SubByteArray(i, data, 20)); for (int j = 0; j < 20; j++) { d[j] ^= k[j]; } ByteHelper.ReplaceBytes(data, i, d); ByteHelper.ReplaceBytes(k, 0, key); } }
/// <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 string GetHash(string input) { var sha1 = SHA1Cng.Create(); var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); return(Convert.ToBase64String(hash)); }
/// <summary> /// CheckSum160 method implementation /// </summary> public static byte[] CheckSum160(string value) { byte[] hash = null; using (SHA1 sha1 = SHA1Cng.Create()) { hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(value)); } return(hash); }
/// <summary> /// CheckSum160 method implementation /// </summary> public static byte[] CheckSum160(byte[] value) { byte[] hash = null; using (SHA1 sha1 = SHA1Cng.Create()) { hash = sha1.ComputeHash(value); } return(hash); }
/// <summary> /// Sets the sha1 data. /// </summary> /// <param name="inputData"> /// The input data. /// </param> /// <param name="record"> /// The record. /// </param> /// <param name="data"> /// The input ds data. /// </param> private static void SetSha1Data(byte[] inputData, DSRecord record, DsInputData data) { byte[] sha1Hash; using (SHA1 sha1 = SHA1Cng.Create()) { sha1Hash = sha1.ComputeHash(inputData); } record.DigestType = DigestType.Sha256; record.KeyTag = Calculate(GeRRData(data)); record.DigestFieldText = sha1Hash.ToHexString(); record.DigestField = sha1Hash; }
/// <summary> /// 计算文件的 sha1 值 /// </summary> /// <param name="path">要计算 MD5 值的文件名和路径</param> /// <returns>MD5 值16进制字符串</returns> public static string GetFileSHA1(string path) { if (System.IO.File.Exists(path) == false) { return(string.Empty); } FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); SHA1 sha1 = SHA1Cng.Create(); byte[] hashByts = sha1.ComputeHash(fs); return(ConvertHashBytes(hashByts)); }
public static Hashes CalculateHash(string filePath) { Hashes res = new Hashes() { sha1 = null, md5 = null }; try { /* * using(var sha1 = SHA1.Create()) * using (var md5 = MD5.Create()) * using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) * { * md5. * hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty); * }*/ using (var md5 = MD5Cng.Create()) // Or MD5Cng.Create using (var sha1 = SHA1Cng.Create()) // Or SHA1Cng.Create using (var input = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { 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); } // 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); res.md5 = BitConverter.ToString(md5.Hash).Replace("-", string.Empty); res.sha1 = BitConverter.ToString(sha1.Hash).Replace("-", string.Empty); } } catch (Exception e) { // This should never happen at this time, but we don't want to stuck the process if it happens. } return(res); }
public byte[] ComputeHash(byte[] input) { switch (SelectedAlgorithm) { case Algorithm.SHA1: return(SHA1Cng.Create().ComputeHash(input)); case Algorithm.SHA256: return(SHA256Cng.Create().ComputeHash(input)); case Algorithm.SHA384: return(SHA384Cng.Create().ComputeHash(input)); case Algorithm.SHA512: return(SHA512Cng.Create().ComputeHash(input)); default: throw new ArgumentException("Valid options are: SHA1, SHA256, SHA384 or SHA512"); } }
public string GetSHA1Cng() { var hasher = SHA1Cng.Create(); return(ComputeHash(hasher)); }
// ReSharper disable AccessToStaticMemberViaDerivedType private void setHashAlgorithm() { switch (cbAlgo.SelectedIndex) { case 0: switch (cbOption.SelectedIndex) { case 0: _ha = SHA256.Create(); break; case 1: _ha = SHA256Cng.Create(); break; case 2: _ha = HMACSHA256.Create(); break; } break; case 1: switch (cbOption.SelectedIndex) { case 0: _ha = SHA512.Create(); break; case 1: _ha = SHA512Cng.Create(); break; case 2: _ha = HMACSHA512.Create(); break; } break; case 2: switch (cbOption.SelectedIndex) { case 0: _ha = SHA1.Create(); break; case 1: _ha = SHA1Cng.Create(); break; case 2: _ha = HMACSHA1.Create(); break; } break; case 3: switch (cbOption.SelectedIndex) { case 0: _ha = MD5.Create(); break; case 1: _ha = MD5Cng.Create(); break; case 2: _ha = HMACMD5.Create(); break; } break; case 4: //stays null for Modbus-CRC16 break; case 5: _ha = new Crc32(); break; } }
protected override HashAlgorithm GetImplementation() { //forcing the issue here to avoid SHA1.Managed being chosen as default (not FIPS-compliant) return(SHA1Cng.Create()); }
/// <summary>ハッシュ(キー無し)サービスプロバイダの生成</summary> /// <param name="eha">ハッシュ(キー無し)サービスプロバイダの列挙型</param> /// <returns>ハッシュ(キー無し)サービスプロバイダ</returns> /// <remarks> /// EnumHashAlgorithmから、HashAlgorithmを生成するために追加。 /// HashAlgorithm.Create(HashNameConst.SHA256) は .NET Core 2 で動作せず。 /// - KeyedHashAlgorithm.Create("HMACSHA1") throw PNSE (on .NET Core 2 /// https://github.com/dotnet/standard/issues/530#issuecomment-375043416 /// </remarks> public static HashAlgorithm CreateHashAlgorithmSP(EnumHashAlgorithm eha) { // ハッシュ(キー無し)サービスプロバイダ HashAlgorithm ha = null; if (eha == EnumHashAlgorithm.Default) { // 既定の暗号化サービスプロバイダ ha = HashAlgorithmCmnFunc.GetHashAlgorithmFromNameString(); // devps(1703) } #region MD5 else if (eha == EnumHashAlgorithm.MD5_CSP) { // MD5CryptoServiceProviderサービスプロバイダ ha = MD5CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.MD5_CNG) { // MD5Cngサービスプロバイダ ha = MD5Cng.Create(); // devps(1703) } #endif #endregion #region RIPEMD160 else if (eha == EnumHashAlgorithm.RIPEMD160_M) { #if NETSTD ha = null; // BouncyCastleを使用する。 #else // RIPEMD160Managedサービスプロバイダ ha = RIPEMD160Managed.Create(); // devps(1703) #endif } #endregion #region SHA1 else if (eha == EnumHashAlgorithm.SHA1_CSP) { // SHA1CryptoServiceProviderサービスプロバイダ ha = SHA1CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.SHA1_CNG) { // SHA1Cngサービスプロバイダ ha = SHA1Cng.Create(); // devps(1703) } #endif else if (eha == EnumHashAlgorithm.SHA1_M) { // SHA1Managedサービスプロバイダ ha = SHA1Managed.Create(); // devps(1703) } #endregion #region SHA256 else if (eha == EnumHashAlgorithm.SHA256_CSP) { // SHA256CryptoServiceProviderサービスプロバイダ ha = SHA256CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.SHA256_CNG) { // SHA256Cngサービスプロバイダ ha = SHA256Cng.Create(); // devps(1703) } #endif else if (eha == EnumHashAlgorithm.SHA256_M) { // SHA256Managedサービスプロバイダ ha = SHA256Managed.Create(); // devps(1703) } #endregion #region SHA384 else if (eha == EnumHashAlgorithm.SHA384_CSP) { // SHA384CryptoServiceProviderサービスプロバイダ ha = SHA384CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.SHA384_CNG) { // SHA384Cngサービスプロバイダ ha = SHA384Cng.Create(); // devps(1703) } #endif else if (eha == EnumHashAlgorithm.SHA384_M) { // SHA384Managedサービスプロバイダ ha = SHA384Managed.Create(); // devps(1703) } #endregion #region SHA512 else if (eha == EnumHashAlgorithm.SHA512_CSP) { // SHA512CryptoServiceProviderサービスプロバイダ ha = SHA512CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.SHA512_CNG) { // SHA512Cngサービスプロバイダ ha = SHA512Cng.Create(); // devps(1703) } #endif else if (eha == EnumHashAlgorithm.SHA512_M) { // SHA512Managedサービスプロバイダ ha = SHA512Managed.Create(); // devps(1703) } #endregion else { // 既定の暗号化サービスプロバイダ ha = HashAlgorithmCmnFunc.GetHashAlgorithmFromNameString(); // devps(1703) } return(ha); }