本文整理汇总了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"));
}
示例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());
}
}
}
示例3: GetHash
public static string GetHash(byte[] bytes)
{
using (var sha1 = new SHA1Managed())
{
return string.Join("", sha1.ComputeHash(bytes).Select(b => b.ToString("x2")));
}
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}
示例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)));
}
示例10: TestSHA1
public void TestSHA1()
{
byte[] data = new byte[256];
byte[] result;
var shaM = new SHA1Managed();
result = shaM.ComputeHash(data);
}
示例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;
}
示例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);
}
示例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;
}
示例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();
}
示例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];
}
}