本文整理汇总了C#中HashAlgorithm.ComputeHash方法的典型用法代码示例。如果您正苦于以下问题:C# HashAlgorithm.ComputeHash方法的具体用法?C# HashAlgorithm.ComputeHash怎么用?C# HashAlgorithm.ComputeHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashAlgorithm
的用法示例。
在下文中一共展示了HashAlgorithm.ComputeHash方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestReusability
public void TestReusability(HashAlgorithm hashAlgorithm)
{
using (hashAlgorithm)
{
byte[] input = { 8, 6, 7, 5, 3, 0, 9, };
byte[] hash1 = hashAlgorithm.ComputeHash(input);
byte[] hash2 = hashAlgorithm.ComputeHash(input);
Assert.Equal(hash1, hash2);
}
}
示例2: Test
static void Test(HashAlgorithm digest)
{
byte[] result1, result2, result3;
if (digest is SHA1) {
Console.WriteLine ("Testing results wrt FIPS 180-1 test vectors");
result1 = new byte [] { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d };
result2 = new byte [] { 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 };
result3 = new byte [] { 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e,
0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f };
} else if (digest is SHA256) {
Console.WriteLine ("Testing results wrt FIPS 180-2 test vectors");
result1 = new byte [] { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad };
result2 = new byte [] { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 };
result3 = new byte [] { 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0 };
} else {
Console.WriteLine ("No test vectors were found.");
return;
}
string input1 = "abc";
string input2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
byte[] input = Encoding.Default.GetBytes (input1);
byte[] output = digest.ComputeHash (input);
Console.WriteLine ("FIPS 180 Test 1: {0}",
BitConverter.ToString (result1) != BitConverter.ToString (output) ?
"FAIL" : "PASS");
input = Encoding.Default.GetBytes (input2);
output = digest.ComputeHash (input);
Console.WriteLine ("FIPS 180 Test 2: {0}",
BitConverter.ToString (result2) != BitConverter.ToString (output) ?
"FAIL" : "PASS");
input = new byte [1000000];
for (int i = 0; i < 1000000; i++)
input[i] = 0x61; // a
output = digest.ComputeHash (input);
Console.WriteLine ("FIPS 180 Test 3: {0}",
BitConverter.ToString (result3) != BitConverter.ToString (output) ?
"FAIL" : "PASS");
}
示例3: VerifyIncrementalResult
private static void VerifyIncrementalResult(HashAlgorithm referenceAlgorithm, IncrementalHash incrementalHash)
{
byte[] referenceHash = referenceAlgorithm.ComputeHash(s_inputBytes);
const int StepA = 13;
const int StepB = 7;
int position = 0;
while (position < s_inputBytes.Length - StepA)
{
incrementalHash.AppendData(s_inputBytes, position, StepA);
position += StepA;
}
incrementalHash.AppendData(s_inputBytes, position, s_inputBytes.Length - position);
byte[] incrementalA = incrementalHash.GetHashAndReset();
Assert.Equal(referenceHash, incrementalA);
// Now try again, verifying both immune to step size behaviors, and that GetHashAndReset resets.
position = 0;
while (position < s_inputBytes.Length - StepB)
{
incrementalHash.AppendData(s_inputBytes, position, StepA);
position += StepA;
}
incrementalHash.AppendData(s_inputBytes, position, s_inputBytes.Length - position);
byte[] incrementalB = incrementalHash.GetHashAndReset();
Assert.Equal(referenceHash, incrementalB);
}
示例4: PrintHash
// prints the hash of a specified input to the console
public static void PrintHash(string name, HashAlgorithm algo, byte[] data)
{
// compute the hash of the input data..
byte[] hash = algo.ComputeHash(data);
// ..and write the hash to the console
Console.WriteLine(name + BytesToHex(hash));
// dispose of the hash algorithm; we do not need to hash more data with it
algo.Clear();
}
示例5: VerifySignature
protected static void VerifySignature(AsymmetricSignatureFormatter formatter, AsymmetricSignatureDeformatter deformatter, HashAlgorithm hashAlgorithm, string hashAlgorithmName)
{
formatter.SetHashAlgorithm(hashAlgorithmName);
deformatter.SetHashAlgorithm(hashAlgorithmName);
byte[] hash = hashAlgorithm.ComputeHash(HelloBytes);
VerifySignatureWithHashBytes(formatter, deformatter, hash);
VerifySignatureWithHashAlgorithm(formatter, deformatter, hashAlgorithm);
}
示例6: GetHash
public static string GetHash(string input, HashAlgorithm hash)
{
byte[] data = hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
示例7: ARC4Crypt
public ARC4Crypt(HashAlgorithm hash, bool isServer)
{
Contract.Requires(hash != null);
var encKey = isServer ? _serverEncClientDec : _serverDecClientEnc;
var decKey = isServer ? _serverDecClientEnc : _serverEncClientDec;
_encrypt = new ARC4Managed();
_decrypt = new ARC4Managed();
_encrypt.Key = hash.ComputeHash(encKey);
_decrypt.Key = hash.ComputeHash(decKey);
var buffer = new byte[DropN];
var length = buffer.Length;
// Drop the first N bytes in the stream, to prevent the FMS attack.
_encrypt.TransformFinalBlock(buffer, 0, length);
_decrypt.TransformFinalBlock(buffer, 0, length);
}
示例8: ComputeHash
public static string ComputeHash(string input, HashAlgorithm algorithm)
{
byte[] salt = Convert.FromBase64String(DEFAULT_SALT);
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
// Combine salt and input bytes
byte[] saltedInput = new Byte[salt.Length + inputBytes.Length];
salt.CopyTo(saltedInput, 0);
inputBytes.CopyTo(saltedInput, salt.Length);
byte[] hashedBytes = algorithm.ComputeHash(saltedInput);
return Convert.ToBase64String(hashedBytes);
}
示例9: GetEmptyHash
private static byte[] GetEmptyHash (HashAlgorithm hash)
{
if (hash is SHA1)
return emptySHA1;
else if (hash is SHA256)
return emptySHA256;
else if (hash is SHA384)
return emptySHA384;
else if (hash is SHA512)
return emptySHA512;
else
return hash.ComputeHash ((byte[])null);
}
示例10: VerifyEmptyHash
public static void VerifyEmptyHash(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithm))
{
for (int i = 0; i < 10; i++)
{
incrementalHash.AppendData(Array.Empty<byte>());
}
byte[] referenceHash = referenceAlgorithm.ComputeHash(Array.Empty<byte>());
byte[] incrementalResult = incrementalHash.GetHashAndReset();
Assert.Equal(referenceHash, incrementalResult);
}
}
示例11: VerifyTrivialHash
public static void VerifyTrivialHash(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithm))
{
byte[] referenceHash = referenceAlgorithm.ComputeHash(Array.Empty<byte>());
byte[] incrementalResult = incrementalHash.GetHashAndReset();
Assert.Equal(referenceHash, incrementalResult);
}
}
示例12: method_10
public byte[] method_10(byte[] byte_2, HashAlgorithm hashAlgorithm_0)
{
Class1.Class13.Enum1 enum1_ = this.method_16(hashAlgorithm_0);
byte[] byte_3 = hashAlgorithm_0.ComputeHash(byte_2);
return this.method_7(byte_3, enum1_);
}
示例13: method_11
public bool method_11(byte[] byte_2, HashAlgorithm hashAlgorithm_0, byte[] byte_3)
{
Class1.Class13.Enum1 enum1_ = this.method_16(hashAlgorithm_0);
byte[] byte_4 = hashAlgorithm_0.ComputeHash(byte_2);
return this.method_9(byte_4, byte_3, enum1_);
}
示例14: ComputeHash
public string ComputeHash(string input, HashAlgorithm algorithm)
{
Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
return BitConverter.ToString(hashedBytes);
}
示例15: RunHash
// Run a hash algorithm test.
protected void RunHash(HashAlgorithm alg, String value, byte[] expected)
{
// Make sure that the hash size is what we expect.
AssertEquals("hash size is incorrect",
alg.HashSize, expected.Length * 8);
// Convert the string form of the input into a byte array.
byte[] input = Encoding.ASCII.GetBytes(value);
// Get the hash value over the input.
byte[] hash = alg.ComputeHash(input);
// Compare the hash with the expected value.
AssertNotNull("returned hash was null", hash);
AssertEquals("hash length is wrong", hash.Length,
expected.Length);
if(!IdenticalBlock(hash, 0, expected, 0, expected.Length))
{
Fail("incorrect hash value produced");
}
// Get the hash value over the input in a sub-buffer.
byte[] input2 = new byte [input.Length + 20];
Array.Copy(input, 0, input2, 10, input.Length);
hash = alg.ComputeHash(input2, 10, input.Length);
// Compare the hash with the expected value.
AssertNotNull("returned hash was null", hash);
AssertEquals("hash length is wrong", hash.Length,
expected.Length);
if(!IdenticalBlock(hash, 0, expected, 0, expected.Length))
{
Fail("incorrect hash value produced");
}
// Get the hash value over the input via a stream.
MemoryStream stream = new MemoryStream(input, false);
hash = alg.ComputeHash(stream);
// Compare the hash with the expected value.
AssertNotNull("returned hash was null", hash);
AssertEquals("hash length is wrong", hash.Length,
expected.Length);
if(!IdenticalBlock(hash, 0, expected, 0, expected.Length))
{
Fail("incorrect hash value produced");
}
}