本文整理汇总了C#中System.Security.Cryptography.SHA1Managed.ComputeHash方法的典型用法代码示例。如果您正苦于以下问题:C# SHA1Managed.ComputeHash方法的具体用法?C# SHA1Managed.ComputeHash怎么用?C# SHA1Managed.ComputeHash使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SHA1Managed
的用法示例。
在下文中一共展示了SHA1Managed.ComputeHash方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例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: GenerateMySQ5LHash
public string GenerateMySQ5LHash(string key)
{
byte[] keyArray = Encoding.UTF8.GetBytes(key);
SHA1Managed enc = new SHA1Managed();
byte[] encodedKey = enc.ComputeHash(enc.ComputeHash(keyArray));
StringBuilder myBuilder = new StringBuilder(encodedKey.Length);
foreach (byte b in encodedKey)
myBuilder.Append(b.ToString("X2"));
return "*" + myBuilder.ToString();
}
示例4: GenerateSHA1Hash
/// <summary>
/// Generate a new hash for RealVault orders
/// </summary>
/// <param name="normalPassword"></param>
/// <param name="merchantId"></param>
/// <param name="orderId"></param>
/// <param name="amount"></param>
/// <param name="currency"></param>
/// <param name="card"></param>
/// <param name="timestamp"></param>
/// <returns></returns>
public static String GenerateSHA1Hash(string hashInput, string sharedSecret)
{
SHA1 sha = new SHA1Managed();
String hashStage1 =
hexEncode(sha.ComputeHash(Encoding.UTF8.GetBytes(hashInput))) + "." +
sharedSecret;
String hashStage2 =
hexEncode(sha.ComputeHash(Encoding.UTF8.GetBytes(hashStage1)));
return hashStage2;
}
示例5: Main
static void Main(string[] args)
{
string pubKey = ConfigurationManager.AppSettings["pubKey"];
string priKey = ConfigurationManager.AppSettings["priKey"];
string input = ConfigurationManager.AppSettings["input"];
string user = ConfigurationManager.AppSettings["user"];
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
SHA1Managed sha = new SHA1Managed();
rsa.FromXmlString(priKey);
Console.WriteLine("begin to encode:");
RSAPKCS1SignatureFormatter signFormatter = new RSAPKCS1SignatureFormatter(rsa);
signFormatter.SetHashAlgorithm("SHA1");
byte[] source = System.Text.ASCIIEncoding.UTF8.GetBytes(input);
byte[] result = sha.ComputeHash(source);
byte[] b = signFormatter.CreateSignature(result);
var signature = Convert.ToBase64String(b);
string outputFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sn", input + ".key");
FileInfo fi = new FileInfo(outputFile);
if (!fi.Directory.Exists)
{
fi.Directory.Create();
}
StreamWriter sw = new StreamWriter(fi.OpenWrite(), Encoding.UTF8);
sw.Write(signature);
sw.Close();
Console.WriteLine();
Console.WriteLine(signature);
using (RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider())
{
rsa2.FromXmlString(pubKey);
RSAPKCS1SignatureDeformatter decodeFormatter = new RSAPKCS1SignatureDeformatter(rsa2);
decodeFormatter.SetHashAlgorithm("SHA1");
byte[] key = Convert.FromBase64String(signature);
byte[] name = sha.ComputeHash(ASCIIEncoding.UTF8.GetBytes(input));
if (decodeFormatter.VerifySignature(name, key))
{
Console.WriteLine("===========can be decoded");
}
else
{
Console.WriteLine("===CANOT be decoded..................");
}
}
}
}
示例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: 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("-", "");
}
示例8: 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);
}
示例9: GetString
public static string GetString(string str, Encoding enc)
{
try
{
using (SHA1Managed sha = new SHA1Managed())
{
byte[] hash = (enc == null) ? sha.ComputeHash(Encoding.UTF8.GetBytes(str)) : sha.ComputeHash(enc.GetBytes(str));
//return Utils.StringHelper.ByteArrayToHexString(hash);
return "";
}
}
catch (Exception ex)
{
throw new CryptoException(ex.Message, ex);
}
}
示例10: Main
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("You need to specify a filename as a single argument");
}
else
{
string file = args[0];
if (File.Exists(file) == false)
{
Console.WriteLine("Argument is not a valid filename");
}
else
{
FileStream fs = new FileStream(@file, FileMode.Open);
using (SHA1Managed sha1 = new SHA1Managed())
{
byte[] hash = sha1.ComputeHash(fs);
StringBuilder sb = new StringBuilder(hash.Length);
foreach (byte b in hash)
{
sb.AppendFormat("{0:X2}", b);
}
Console.WriteLine(sb.ToString());
}
}
}
}
示例11: GenerateId
public virtual string GenerateId(string handle)
{
var sha = new SHA1Managed();
var handleBytes = Encoding.UTF8.GetBytes(handle);
var hashBytes = sha.ComputeHash(handleBytes);
return BitConverter.ToString(hashBytes, 0, 9).Replace("-", "");
}
示例12: EncryptString
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="inputStr">输入字符串</param>
/// <param name="keyStr">密码,可以为“”</param>
/// <returns>输出加密后字符串</returns>
public string EncryptString(string inputStr, string keyStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (keyStr == "")
keyStr = key;
byte[] inputByteArray = Encoding.Default.GetBytes(inputStr);
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();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
cs.Close();
ms.Close();
return ret.ToString();
}
示例13: CreateWsseHeader
private String CreateWsseHeader()
{
// HTTPリクエスト毎に生成するセキュリティ・トークン(ランダム文字列)
byte[] b_nonce = new byte[8];
Random rand = new Random();
rand.NextBytes( b_nonce );
// nonce 生成時の日
string created = DateTime.Now.ToUniversalTime().ToString( "o" );
byte[] b_created = Encoding.UTF8.GetBytes( created );
byte[] b_password = Encoding.UTF8.GetBytes( Password );
SHA1Managed sh1 = new SHA1Managed();
sh1.Initialize();
// Join
byte[] origin = new byte[b_nonce.Length + b_created.Length + b_password.Length];
Array.Copy( b_nonce, 0, origin, 0, b_nonce.Length );
Array.Copy( b_created, 0, origin, b_nonce.Length, b_created.Length );
Array.Copy( b_password, 0, origin, b_nonce.Length + b_created.Length, b_password.Length );
// Create Hash Value
byte[] passwordDigest = sh1.ComputeHash( origin );
string header = String.Format(
"UsernameToken Username=\"{0}\", PasswordDigest=\"{1}\", Nonce=\"{2}\", Created=\"{3}\"",
Username, Convert.ToBase64String( passwordDigest ), Convert.ToBase64String( b_nonce ), created );
return header;
}
示例14: 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);
}
示例15: 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();
}