• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Cryptography.SHA1Managed类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Security.Cryptography.SHA1Managed的典型用法代码示例。如果您正苦于以下问题:C# SHA1Managed类的具体用法?C# SHA1Managed怎么用?C# SHA1Managed使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



SHA1Managed类属于System.Security.Cryptography命名空间,在下文中一共展示了SHA1Managed类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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


示例16: Hash

 }/*
 private string Hash(byte[] buffer)
 {
     SHA1Managed sha1 = new SHA1Managed();
     byte[] hashBytes = sha1.ComputeHash(buffer);
     return BufferToString(hashBytes);
 }
 private string Hash(FileStream stream)
 {
     SHA1Managed sha1 = new SHA1Managed();
     byte[] hashBytes = sha1.ComputeHash(stream);
     return BufferToString(hashBytes);
 }
 // TODO: refactor, this is a copy/paste from ObjectForScripting
 private string Hash(FileInfo fileInfo)
 {
     SHA1Managed sha1 = new SHA1Managed();
     FileStream inFile = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
     return Hash(inFile);
 }*/
 private string FileHash(byte[] buffer)
 {
     MemoryStream ms = new MemoryStream(buffer,false);
     SHA1Managed sha1 = new SHA1Managed();
     return string.Concat(from b in sha1.ComputeHash(ms)
                          select b.ToString("X2"));
 }
开发者ID:cpbuck12,项目名称:Db,代码行数:27,代码来源:DB.cs


示例17: CalculateSHA1

 internal static string CalculateSHA1(string text)
 {
     SHA1Managed s = new SHA1Managed();
     UTF8Encoding enc = new UTF8Encoding();
     s.ComputeHash(enc.GetBytes(text.ToCharArray()));
     return BitConverter.ToString(s.Hash).Replace("-", "");
 }
开发者ID:LiveCourse,项目名称:LiveCourse,代码行数:7,代码来源:SHA1.cs


示例18: GenerateSHA1Hash

 public string GenerateSHA1Hash(string str)
 {
     SHA1Managed sha1 = new SHA1Managed();
     var hash = sha1.ComputeHash(StringToAscii(str));
     string hex = BitConverter.ToString(hash);
     return hex.Replace("-", "").ToLower();
 }
开发者ID:swizzley,项目名称:fh-dotnet-sdk,代码行数:7,代码来源:HashService.cs


示例19: ReadRecord

        public static TapeRecord ReadRecord(Stream file)
        {
            ReadAndVerifySignature(file, ReadableHeaderStart, "Start");
            var dataLength = ReadReadableInt64(file);
            ReadAndVerifySignature(file, ReadableHeaderEnd, "Header-End");

            var data = new byte[dataLength];
            file.Read(data, 0, (int) dataLength);

            ReadAndVerifySignature(file, ReadableFooterStart, "Footer-Start");

            ReadReadableInt64(file); //length verified
            var recVersion = ReadReadableInt64(file);
            var hash = ReadReadableHash(file);
            using (var managed = new SHA1Managed())
            {

                var computed = managed.ComputeHash(data);

                if (!computed.SequenceEqual(hash))
                    throw new InvalidOperationException("Hash corrupted");
            }
            ReadAndVerifySignature(file, ReadableFooterEnd, "End");

            return new TapeRecord(recVersion, data);
        }
开发者ID:JeetKunDoug,项目名称:lokad-cqrs,代码行数:26,代码来源:TapeStreamSerializer.cs


示例20: GetSHA1

 /// <summary>
 /// SHA1非对称加密.用于密码加密,使用SHA1Managed类产生长度为160位哈希值.
 /// </summary>
 /// <param name="text">传入一个任意长字符串.</param>
 /// <returns>返回长度为28字节的字符串.</returns>
 public static string GetSHA1(string text)
 {
     byte[] byteSHA1 = System.Text.Encoding.UTF8.GetBytes(text);
     var sha1 = new SHA1Managed();
     byte[] result = sha1.ComputeHash(byteSHA1);
     return Convert.ToBase64String(result);
 }
开发者ID:441023065,项目名称:CMS,代码行数:12,代码来源:EncryptStringHelper.cs



注:本文中的System.Security.Cryptography.SHA1Managed类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Cryptography.SHA256CryptoServiceProvider类代码示例发布时间:2022-05-26
下一篇:
C# Cryptography.SHA1CryptoServiceProvider类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap