當前位置: 首頁>>代碼示例>>C#>>正文


C# Cryptography.SHA1Managed類代碼示例

本文整理匯總了C#中System.Security.Cryptography.SHA1Managed的典型用法代碼示例。如果您正苦於以下問題:C# SHA1Managed類的具體用法?C# SHA1Managed怎麽用?C# SHA1Managed使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


SHA1Managed類屬於System.Security.Cryptography命名空間,在下文中一共展示了SHA1Managed類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: GetSha1

        private static string GetSha1(string value)
        {
            var data = Encoding.ASCII.GetBytes(value);
            var hashData = new SHA1Managed().ComputeHash(data);

            return hashData.Aggregate(string.Empty, (current, b) => current + b.ToString("X2"));
        }
開發者ID:NasuTek,項目名稱:NasuTek-Inventory-Services,代碼行數:7,代碼來源:UserEdit.cs

示例2: GenerateKeyDataFromNonces

        public static AESKeyData GenerateKeyDataFromNonces(byte[] serverNonce, byte[] newNonce) {
            using (SHA1 hash = new SHA1Managed()) {
                var nonces = new byte[48];

                newNonce.CopyTo(nonces, 0);
                serverNonce.CopyTo(nonces, 32);
                byte[] hash1 = hash.ComputeHash(nonces);

                serverNonce.CopyTo(nonces, 0);
                newNonce.CopyTo(nonces, 16);
                byte[] hash2 = hash.ComputeHash(nonces);

                nonces = new byte[64];
                newNonce.CopyTo(nonces, 0);
                newNonce.CopyTo(nonces, 32);
                byte[] hash3 = hash.ComputeHash(nonces);

                using (var keyBuffer = new MemoryStream(32))
                using (var ivBuffer = new MemoryStream(32)) {
                    keyBuffer.Write(hash1, 0, hash1.Length);
                    keyBuffer.Write(hash2, 0, 12);

                    ivBuffer.Write(hash2, 12, 8);
                    ivBuffer.Write(hash3, 0, hash3.Length);
                    ivBuffer.Write(newNonce, 0, 4);

                    return new AESKeyData(keyBuffer.ToArray(), ivBuffer.ToArray());
                }
            }
        }
開發者ID:ra0o0f,項目名稱:TLSharp,代碼行數:30,代碼來源:AES.cs

示例3: GetHash

 public static string GetHash(byte[] bytes)
 {
     using (var sha1 = new SHA1Managed())
     {
         return string.Join("", sha1.ComputeHash(bytes).Select(b => b.ToString("x2")));
     }
 }
開發者ID:jbijlsma,項目名稱:JsTestAdapter,代碼行數:7,代碼來源:Sha1Utils.cs

示例4: Transform

		public override string Transform(string key)
		{
			SHA1Managed sh = new SHA1Managed();
			byte[] data = sh.ComputeHash(Encoding.Unicode.GetBytes(key));

			return Convert.ToBase64String(data, Base64FormattingOptions.None);
		}
開發者ID:623442733,項目名稱:EnyimMemcached,代碼行數:7,代碼來源:SHA1KeyTransformer.cs

示例5: AddFile

        public void AddFile(DeduplicatorState state, FileInfo sourceFile, string destinationPath)
        {
            if (state.DestinationToFileHash.ContainsKey(destinationPath))
            {
                // File has already been added.
                return;
            }

            // Read the source file.
            var memory = new MemoryStream();
            using (var stream = new BufferedStream(new FileStream(sourceFile.FullName, FileMode.Open, FileAccess.Read, FileShare.None), 1200000))
            {
                stream.CopyTo(memory);
            }

            // Hash the memory stream.
            var sha1 = new SHA1Managed();
            memory.Seek(0, SeekOrigin.Begin);
            var hashBytes = sha1.ComputeHash(memory);
            var hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
            memory.Seek(0, SeekOrigin.Begin);

            // Add to the file hash -> source map if not already present.
            if (!state.FileHashToSource.ContainsKey(hashString))
            {
                state.FileHashToSource.Add(hashString, memory);
            }
            else
            {
                memory.Dispose();
            }

            state.DestinationToFileHash.Add(destinationPath, hashString);
        }
開發者ID:marler8997,項目名稱:Protobuild,代碼行數:34,代碼來源:Deduplicator.cs

示例6: EncryptFile

 public bool EncryptFile(string filePath, string savePath, string keyStr)
 {
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     if (keyStr == "")
         keyStr = key;
     FileStream fs = File.OpenRead(filePath);
     byte[] inputByteArray = new byte[fs.Length];
     fs.Read(inputByteArray, 0, (int)fs.Length);
     fs.Close();
     byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
     SHA1 ha = new SHA1Managed();
     byte[] hb = ha.ComputeHash(keyByteArray);
     sKey = new byte[8];
     sIV = new byte[8];
     for (int i = 0; i < 8; i++)
         sKey[i] = hb[i];
     for (int i = 8; i < 16; i++)
         sIV[i - 8] = hb[i];
     des.Key = sKey;
     des.IV = sIV;
     MemoryStream ms = new MemoryStream();
     CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
     cs.Write(inputByteArray, 0, inputByteArray.Length);
     cs.FlushFinalBlock();
     fs = File.OpenWrite(savePath);
     foreach (byte b in ms.ToArray())
     {
         fs.WriteByte(b);
     }
     fs.Close();
     cs.Close();
     ms.Close();
     return true;
 }
開發者ID:devworker55,項目名稱:Mammatus,代碼行數:34,代碼來源:MySecurity.cs

示例7: DiscoInfoToVersion

 /// <summary>
 /// Empaqquete un DIscoInfo
 /// </summary>
 /// <param name="info">DicoInfo</param>
 /// <returns>Ampaquetage</returns>
 public static string DiscoInfoToVersion(DiscoInfo info)
 {
     StringBuilder builder = new StringBuilder(256);
     DiscoIdentity[] identities = info.GetIdentities();
     string[] ids = new string[identities.Length];
     for (int i = 0; i < identities.Length; i++)
     {
         ids[i] = string.Format("{0}/{1}", identities[i].Category, identities[i].Type);
     }
     Array.Sort(ids);
     foreach (string id in ids)
     {
         builder.AppendFormat("{0}<", id);
     }
     DiscoFeature[] features = info.GetFeatures();
     string[] feas = new string[features.Length];
     for (int i = 0; i < features.Length; i++)
     {
         feas[i] = features[i].Var;
     }
     Array.Sort(feas);
     foreach (string fea in feas)
     {
         builder.AppendFormat("{0}<", fea);
     }
     SHA1Managed sha1 = new SHA1Managed();
     byte[] hash = sha1.ComputeHash(Encoding.Unicode.GetBytes(builder.ToString()));
     return Convert.ToBase64String(hash);
 }
開發者ID:spzenk,項目名稱:sfdocsamples,代碼行數:34,代碼來源:Query.cs

示例8: HashValues

        private string HashValues(string Value1, string Value2, string HashingAlgorithm)
        {
            string sHashingAlgorithm = "";
            if (String.IsNullOrEmpty(HashingAlgorithm))
                sHashingAlgorithm = "SHA-1";
            else
                sHashingAlgorithm = HashingAlgorithm;

            byte[] arrByte;

            if (sHashingAlgorithm == "SHA-1")
            {
                SHA1Managed hash = new SHA1Managed();
                arrByte = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(Value1 + Value2));
            }
            else
            {
                SHA256Managed hash = new SHA256Managed();
                arrByte = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(Value1 + Value2));
            }

            string s = "";
            for (int i = 0; i < arrByte.Length; i++)
            {
                s += arrByte[i].ToString("x2");
            }
            return s;
        }
開發者ID:ArfiSoft,項目名稱:MSoHelpDesk,代碼行數:28,代碼來源:KaseyaHelper.cs

示例9: CompareStringToHash

        public bool CompareStringToHash(string s, string hash)
        {
            var hashData = Convert.FromBase64String(hash);
            // First, pluck the four-byte salt off of the end of the hash
            var saltData = new byte[SaltSize];
            Array.Copy(hashData, hashData.Length - saltData.Length, saltData, 0, saltData.Length);

            var passwordData = Encoding.UTF8.GetBytes(s);

            // Append the salt to the end of the original
            var saltedPasswordData = new byte[passwordData.Length + saltData.Length];
            Array.Copy(passwordData, 0, saltedPasswordData, 0, passwordData.Length);
            Array.Copy(saltData, 0, saltedPasswordData, passwordData.Length, saltData.Length);

            // Create a new SHA-1 instance and compute the hash
            var sha = new SHA1Managed();
            var newHashData = sha.ComputeHash(saltedPasswordData);

            // Add salt bytes onto end of the original hash for storage
            var newHashSaltData = new byte[newHashData.Length + saltData.Length];
            Array.Copy(newHashData, 0, newHashSaltData, 0, newHashData.Length);
            Array.Copy(saltData, 0, newHashSaltData, newHashData.Length, saltData.Length);
            // Compare and return
            return (Convert.ToBase64String(hashData).Equals(Convert.ToBase64String(newHashSaltData)));
        }
開發者ID:gan123,項目名稱:Right-Recruit,代碼行數:25,代碼來源:Hasher.cs

示例10: TestSHA1

 public void TestSHA1()
 {
     byte[] data = new byte[256];
     byte[] result;
     var shaM = new SHA1Managed();
     result = shaM.ComputeHash(data);
 }
開發者ID:Zino2201,項目名稱:Cosmos,代碼行數:7,代碼來源:Kernel.cs

示例11: CreateUltraSecureStream

        public static SeekableCryptoStream CreateUltraSecureStream(string pswd, int seglen, Stream btr)
        {
            SHA1 mt = new SHA1Managed();
            byte[] hash = mt.ComputeHash(Encoding.UTF8.GetBytes(pswd));
            List<char> tlist = new List<char>(pswd.ToCharArray());
            tlist.Reverse();
            string reversed = new string(tlist.ToArray());
            //Create stream 0
            Aes rdale = new AesManaged();

            Rfc2898DeriveBytes derivitive = new Rfc2898DeriveBytes(reversed + pswd + hash[5].ToString(), hash);
            rdale.Key = derivitive.GetBytes(32);
            derivitive = new Rfc2898DeriveBytes(hash, Encoding.Unicode.GetBytes(pswd), 16);
            rdale.IV = derivitive.GetBytes(16);
            SeekableCryptoStream mstr = new SeekableCryptoStream(new SegmentReader(btr, rdale, seglen));
            //Create stream 1
            Aes secondale = new AesManaged();
            derivitive = new Rfc2898DeriveBytes(pswd + reversed + hash[2].ToString(), Encoding.BigEndianUnicode.GetBytes(pswd));
            secondale.Key = derivitive.GetBytes(32);
            derivitive = new Rfc2898DeriveBytes(hash, Encoding.Unicode.GetBytes(pswd), 16);
            secondale.IV = derivitive.GetBytes(16);
            //No clue why this has to be less than 16384, but it does....
            Console.WriteLine("WARNING: Insecure test algorithm. Uncomment line below to secure it");
           //TODO: DOESN't work with 2 for some reason....
            mstr = new SeekableCryptoStream(new SegmentReader(mstr, rdale, seglen));
            return mstr;
        }
開發者ID:IDWMaster,項目名稱:IDWOS2012,代碼行數:27,代碼來源:crypthelpers.cs

示例12: FromFile

 public static Hash FromFile(string path)
 {
     SHA1 sha = new SHA1Managed();
     byte[] arr = FileUtils.GetAllBytes(path);
     byte[] result = sha.ComputeHash(arr);
     return new Hash(result);
 }
開發者ID:hach-que,項目名稱:Pivot.Update,代碼行數:7,代碼來源:Hash.cs

示例13: DecryptFile2

        ///
        /// 圖片解密
        ///
        /// 源文件
        /// 保存文件
        /// 密鑰
        public static byte[] DecryptFile2(byte[] data, string keyStr)
        {
            byte[] inputByteArray;

            //通過des解密
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = data;
            byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
            //定義哈希變量
            SHA1 ha = new SHA1Managed();
            //計算指定字節組指定區域哈希值
            byte[] hb = ha.ComputeHash(keyByteArray);
            //加密密鑰數組
            byte[] sKey = new byte[8];
            //加密變量
            byte[] sIV = new byte[8];
            for (int i = 0; i < 8; i++)
            sKey[i] = hb[i];
            for (int i = 8; i < 16; i++)
                sIV[i - 8] = hb[i];
            //獲取加密密鑰
            des.Key = sKey;
            //加密變量
            des.IV = sIV;
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            inputByteArray = ms.ToArray();
            cs.Close();
            ms.Close();
            return inputByteArray;
        }
開發者ID:VKeCRM,項目名稱:V2,代碼行數:39,代碼來源:DEncrypt4ImageHelper.cs

示例14: WardenRandom

        public WardenRandom(byte[] seed)
        {
            Data1 = new byte[0x14];
            Data2 = new byte[0x14];
            Data3 = new byte[0x14];

            int length1 = (int)seed.Length >> 1;
            int length2 = seed.Length - length1;

            byte[] seed1 = new byte[length1];
            byte[] seed2 = new byte[length2];

            for (int i = 0; i < length1; i++)
                seed1[i] = seed[i];
            for (int i = 0; i < length2; i++)
                seed2[i] = seed[i + length1];

            SHA1 sha = new SHA1Managed();
            Data2 = sha.ComputeHash(seed1);
            Data3 = sha.ComputeHash(seed2);

            sha.Initialize();
            sha.TransformBlock(Data2, 0, Data2.Length, Data2, 0);
            sha.TransformBlock(Data1, 0, Data1.Length, Data1, 0);
            sha.TransformFinalBlock(Data3, 0, Data3.Length);

            Data1 = sha.Hash;

            sha.Initialize();
        }
開發者ID:Mofsy,項目名稱:jinxbot,代碼行數:30,代碼來源:WardenRandom.cs

示例15: GenKey

        static void GenKey(string baseName, out byte[] desKey, out byte[] desIV)
        {
            byte[] secretBytes = { 6, 29, 66, 6, 2, 68, 4, 7, 70 };

            byte[] baseNameBytes = new ASCIIEncoding().GetBytes(baseName);

            byte[] hashBytes = new byte[secretBytes.Length + baseNameBytes.Length];

            // copy secret byte to start of hash array
            for (int i = 0; i < secretBytes.Length; i++)
            {
                hashBytes[i] = secretBytes[i];
            }

            // copy filename byte to end of hash array
            for (int i = 0; i < baseNameBytes.Length; i++)
            {
                hashBytes[i + secretBytes.Length] = baseNameBytes[i];
            }

            SHA1Managed sha = new SHA1Managed();

            // run the sha1 hash
            byte[] hashResult = sha.ComputeHash(hashBytes);

            desKey = new byte[8];
            desIV = new byte[8];

            for (int i = 0; i < 8; i++)
            {
                desKey[i] = hashResult[i];
                desIV[i] = hashResult[8 + i];
            }
        }
開發者ID:ufosky-server,項目名稱:MultiversePlatform,代碼行數:34,代碼來源:Program.cs


注:本文中的System.Security.Cryptography.SHA1Managed類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。