本文整理汇总了C#中System.Security.Cryptography.HashAlgorithm类的典型用法代码示例。如果您正苦于以下问题:C# HashAlgorithm类的具体用法?C# HashAlgorithm怎么用?C# HashAlgorithm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HashAlgorithm类属于System.Security.Cryptography命名空间,在下文中一共展示了HashAlgorithm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MD5Support
/// <summary>
/// Creates a message digest using the specified name to set Algorithm property.
/// </summary>
/// <param name="algorithm">The name of the algorithm to use</param>
public MD5Support(string algorithm)
{
var algorithmName = algorithm.Equals("SHA-1") ? "SHA" : algorithm;
_algorithm = (HashAlgorithm) CryptoConfig.CreateFromName(algorithmName);
data = new byte[0];
_position = 0;
}
示例2: VartotojaiController
public VartotojaiController(IAuthenticationProvider authenticationProvider, ISessionFactory sessionFactory, [LoggedIn] UserInformation loggedInUser, HashAlgorithm hashAlgorithm)
{
_authenticationProvider = authenticationProvider;
_sessionFactory = sessionFactory;
_loggedInUser = loggedInUser;
_hashAlgorithm = hashAlgorithm;
}
示例3: WriteHash
public void WriteHash(HashAlgorithm hash, DocPosition docPos, AncestralNamespaceContextManager anc)
{
if (this.IsInNodeSet)
{
byte[] bytes;
UTF8Encoding encoding = new UTF8Encoding(false);
if (docPos == DocPosition.AfterRootElement)
{
bytes = encoding.GetBytes("(char) 10");
hash.TransformBlock(bytes, 0, bytes.Length, bytes, 0);
}
bytes = encoding.GetBytes("<?");
hash.TransformBlock(bytes, 0, bytes.Length, bytes, 0);
bytes = encoding.GetBytes(this.Name);
hash.TransformBlock(bytes, 0, bytes.Length, bytes, 0);
if ((this.Value != null) && (this.Value.Length > 0))
{
bytes = encoding.GetBytes(" " + this.Value);
hash.TransformBlock(bytes, 0, bytes.Length, bytes, 0);
}
bytes = encoding.GetBytes("?>");
hash.TransformBlock(bytes, 0, bytes.Length, bytes, 0);
if (docPos == DocPosition.BeforeRootElement)
{
bytes = encoding.GetBytes("(char) 10");
hash.TransformBlock(bytes, 0, bytes.Length, bytes, 0);
}
}
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:29,代码来源:CanonicalXmlProcessingInstruction.cs
示例4: GetCode
internal static int GetCode(
HashAlgorithm algorithm,
string secret,
long counter,
int digits)
{
Contract.Requires<ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
Contract.Requires<ArgumentOutOfRangeException>(algorithm != HashAlgorithm.Unknown);
Contract.Requires<ArgumentNullException>(secret != null);
Contract.Requires<ArgumentOutOfRangeException>(counter >= 0);
Contract.Requires<ArgumentOutOfRangeException>(digits > 0);
Contract.Ensures(Contract.Result<int>() > 0);
Contract.Ensures(Contract.Result<int>() < Math.Pow(10, digits));
var generator = HMAC.Create(algorithm.ToAlgorithmName());
generator.Key = Encoding.ASCII.GetBytes(secret);
generator.ComputeHash(CounterToBytes(counter));
var hmac =
generator
.Hash
.Select(b => Convert.ToInt32(b))
.ToArray();
var offset = hmac[19] & 0xF;
var code =
(hmac[offset + 0] & 0x7F) << 24
| (hmac[offset + 1] & 0xFF) << 16
| (hmac[offset + 2] & 0xFF) << 8
| (hmac[offset + 3] & 0xFF);
return code % (int) Math.Pow(10, digits);
}
示例5: StringEncryption
/// <summary>
/// Initializes a new StringEncryption instance.
/// </summary>
/// <param name="bulkCipher">The bulk cipher algorithm to use.</param>
/// <param name="hash">The hash algorithm to use.</param>
/// <exception cref="ArgumentNullException">One of the parameters is a null reference.</exception>
public StringEncryption(SymmetricAlgorithm bulkCipher, HashAlgorithm hash) {
if (bulkCipher == null)
throw new ArgumentNullException("bulkCipher", ResourceController.GetString("Error_ParamNull"));
if (hash == null)
throw new ArgumentNullException("hash", ResourceController.GetString("Error_ParamNull"));
Init(bulkCipher, hash);
}
示例6: TestSha
private bool TestSha(HashAlgorithm alg1, HashAlgorithm alg2)
{
string tstStr = "This is a string that I will be getting the hash of!";
byte[] testHash = System.Text.UTF8Encoding.UTF8.GetBytes(tstStr);
byte[] hash1 = alg1.ComputeHash(testHash, 0, testHash.Length);
byte[] hash2 = alg1.ComputeHash(testHash, 0, testHash.Length);
byte[] hash3 = alg1.ComputeHash(testHash, 0, testHash.Length - 1);
byte[] hash4 = alg2.ComputeHash(testHash);
byte[] hash5 = alg2.ComputeHash(testHash, 0, testHash.Length - 1);
if (hash1.Length != (alg1.HashSize/8)) throw new Exception();
bool res1 = true, res2 = true, res3 = true, res4 = true;
for (int i = 0; i < hash1.Length; i++)
{
res1 &= (hash1[i] == hash2[i]);
res2 &= (hash1[i] == hash3[i]);
res3 &= (hash1[i] == hash4[i]);
res4 &= (hash3[i] == hash5[i]);
}
return res1 && !res2 && res3 && res4;
}
示例7: computeHash
public static string computeHash(string password, HashAlgorithm algorithm)
{
Byte[] inputBytes = Encoding.UTF8.GetBytes(password);
Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
return BitConverter.ToString(hashedBytes);
}
示例8: HashingStreamEx
public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm hashAlgorithm)
{
if(sBaseStream == null) throw new ArgumentNullException("sBaseStream");
m_sBaseStream = sBaseStream;
m_bWriting = bWriting;
#if !KeePassLibSD
m_hash = (hashAlgorithm ?? new SHA256Managed());
#else // KeePassLibSD
m_hash = null;
try { m_hash = HashAlgorithm.Create("SHA256"); }
catch(Exception) { }
try { if(m_hash == null) m_hash = HashAlgorithm.Create(); }
catch(Exception) { }
#endif
if(m_hash == null) { Debug.Assert(false); return; }
// Validate hash algorithm
if(!m_hash.CanReuseTransform || !m_hash.CanTransformMultipleBlocks)
{
Debug.Assert(false);
m_hash = null;
}
}
示例9: ComputeHash
public void ComputeHash(HashAlgorithm hash)
{
byte[] tmp = Encoding.UTF8.GetBytes (_name);
hash.TransformBlock (tmp, 0, tmp.Length, null, 0);
tmp = Encoding.UTF8.GetBytes (_body);
hash.TransformBlock (tmp, 0, tmp.Length, null, 0);
}
示例10: SaltedHash
/// <summary>
/// Initializes a new instance of the <see cref="SaltedHash"/> class.
/// </summary>
/// <param name="hashAlgorithm">A <see cref="HashAlgorithm"/> HashAlgorihm which is derived from HashAlgorithm. C# provides
/// the following classes: SHA1Managed,SHA256Managed, SHA384Managed, SHA512Managed and MD5CryptoServiceProvider</param>
/// <param name="saltLength">Length of the salt.</param>
public SaltedHash(HashAlgorithm hashAlgorithm, int saltLength)
{
Check.NotNull(hashAlgorithm, nameof(hashAlgorithm));
Check.Positive(saltLength, nameof(saltLength));
this.hashAlgorithm = hashAlgorithm;
this.saltLength = saltLength;
}
示例11: CalculateHash
/// <summary>
/// File content hash calculation
/// </summary>
/// <example>
/// <code>
/// // Implementation of <see cref="CalculateSha256Hash"/>
/// public static QuickIOHashResult CalculateSha256Hash( QuickIOPathInfo pathInfo )
/// {
/// using ( var fs = OpenRead( pathInfo ) )
/// using ( var hashAlgorithm = SHA256.Create( ) )
/// {
/// return CalculateHash( hashAlgorithm, fs );
/// }
/// }
/// </code>
/// </example>
/// <returns><see cref="QuickIOHashResult"/></returns>
public static QuickIOHashResult CalculateHash( HashAlgorithm hashAlgorithm, Stream stream )
{
Contract.Requires( hashAlgorithm != null );
Contract.Requires( stream != null );
return new QuickIOHashResult( hashAlgorithm.ComputeHash( stream ) );
}
示例12: StartTests
protected int StartTests(HashAlgorithm hash, byte[] input, byte[] result)
{
try {
byte[] ch = hash.ComputeHash(input, 0, input.Length);
if (!ArrayEquals(ch, result))
AddError("HB-ST1");
} catch {
AddError("HB-ST2");
}
try {
// feed input byte-by-byte
for(int i = 0; i < input.Length - 1; i++) {
hash.TransformBlock(input, i, 1, input, i);
}
if (input.Length > 0)
hash.TransformFinalBlock(input, input.Length - 1, 1);
else
hash.TransformFinalBlock(input, 0, 0);
if (!ArrayEquals(hash.Hash, result)) {
AddError("HB-ST3");
Console.WriteLine(Encoding.ASCII.GetString(input));
}
} catch {
AddError("HB-ST4");
} finally {
hash.Initialize();
}
return 4;
}
示例13: CalculateHa1
internal void CalculateHa1(string realm, HashAlgorithm hashAlgorithm)
{
Ha1 =
Helpers.HexEncode(
hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(string.Format("{0}:{1}:{2}", Username, realm, _password))));
_password = null;
}
示例14: AssemblyHash
/// <summary>
/// Constructor
/// </summary>
/// <remarks>If <paramref name="hashAlgo"/> is an unsupported hash algorithm, then
/// <see cref="AssemblyHashAlgorithm.SHA1"/> will be used as the hash algorithm.</remarks>
/// <param name="hashAlgo">The algorithm to use</param>
public AssemblyHash(AssemblyHashAlgorithm hashAlgo) {
switch (hashAlgo) {
case AssemblyHashAlgorithm.MD5:
hasher = MD5.Create();
break;
case AssemblyHashAlgorithm.None:
case AssemblyHashAlgorithm.MD2:
case AssemblyHashAlgorithm.MD4:
case AssemblyHashAlgorithm.SHA1:
case AssemblyHashAlgorithm.MAC:
case AssemblyHashAlgorithm.SSL3_SHAMD5:
case AssemblyHashAlgorithm.HMAC:
case AssemblyHashAlgorithm.TLS1PRF:
case AssemblyHashAlgorithm.HASH_REPLACE_OWF:
default:
hasher = SHA1.Create();
break;
case AssemblyHashAlgorithm.SHA_256:
hasher = SHA256.Create();
break;
case AssemblyHashAlgorithm.SHA_384:
hasher = SHA384.Create();
break;
case AssemblyHashAlgorithm.SHA_512:
hasher = SHA512.Create();
break;
}
}
示例15: ComputeHash
/// <summary>Compute hash on input string</summary>
/// <param name="file">The file to get hash from.</param>
/// <param name="algorithm"> </param>
/// <returns>The hash as a hexadecimal String.</returns>
public static string ComputeHash(this FileInfo file, HashAlgorithm algorithm)
{
using (var stream = new BufferedStream(file.OpenRead(), 1024 * 8))
{
return ComputeHash(stream, algorithm);
}
}