本文整理汇总了C#中HashAlgorithm类的典型用法代码示例。如果您正苦于以下问题:C# HashAlgorithm类的具体用法?C# HashAlgorithm怎么用?C# HashAlgorithm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HashAlgorithm类属于命名空间,在下文中一共展示了HashAlgorithm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WinDbFiller
public WinDbFiller(IFingerprintService fingerprintService, IWorkUnitBuilder workUnitBuilder, ITagService tagService)
{
this.fingerprintService = fingerprintService;
this.workUnitBuilder = workUnitBuilder;
this.tagService = tagService;
InitializeComponent();
Icon = Resources.Sound;
foreach (object item in ConfigurationManager.ConnectionStrings)
{
_cmbDBFillerConnectionString.Items.Add(item.ToString());
}
if (_cmbDBFillerConnectionString.Items.Count > 0)
{
_cmbDBFillerConnectionString.SelectedIndex = 0;
}
_btnStart.Enabled = false;
_btnStop.Enabled = false;
_nudThreads.Value = MaxThreadToProcessFiles;
_pbTotalSongs.Visible = false;
hashAlgorithm = 0; /**/
_lbAlgorithm.SelectedIndex = 0; /*Set default algorithm LSH*/
if (hashAlgorithm == HashAlgorithm.LSH)
{
_nudHashKeys.ReadOnly = false;
_nudHashTables.ReadOnly = false;
}
object[] items = Enum.GetNames(typeof (StrideType)); /*Add enumeration types in the combo box*/
_cmbStrideType.Items.AddRange(items);
_cmbStrideType.SelectedIndex = 0;
}
示例2: WinDBFiller
/// <summary>
/// Constructor
/// </summary>
public WinDBFiller()
{
InitializeComponent();
Icon = Resources.Sound;
foreach (object item in ConfigurationManager.ConnectionStrings) /*Detect all the connection strings*/
_cmbDBFillerConnectionString.Items.Add(item.ToString());
if (_cmbDBFillerConnectionString.Items.Count > 0)
_cmbDBFillerConnectionString.SelectedIndex = 0;
_btnStart.Enabled = false;
_btnStop.Enabled = false;
_nudThreads.Value = THREADS;
_pbTotalSongs.Visible = false;
_hashAlgorithm = 0; /**/
_lbAlgorithm.SelectedIndex = 0; /*Set default algorithm LSH*/
if (_hashAlgorithm == HashAlgorithm.LSH)
{
_nudHashKeys.ReadOnly = false;
_nudHashTables.ReadOnly = false;
}
string[] items = Enum.GetNames(typeof (StrideType)); /*Add enumeration types in the combo box*/
_cmbStrideType.Items.AddRange(items);
_cmbStrideType.SelectedIndex = 0;
}
示例3: Main
static void Main(string[] args)
{
if (args.Length == 0 || args.Length > 1) {
Console.WriteLine("\r\nUsage: hash [filename]");
return;
}
HashAlgorithm[] hashes = new HashAlgorithm[] {
new MD2CryptoServiceProvider(), new MD5CryptoServiceProvider(),
new SHA1CryptoServiceProvider(), new Org.Mentalis.Security.Cryptography.RIPEMD160Managed()
};
string[] names = new string[] {"MD2: ", "MD5: ", "SHA1: ", "RIPEMD160: "};
byte[] buffer = new byte[4096];
FileStream fs = File.Open(args[0], FileMode.Open, FileAccess.Read, FileShare.Read);
int size = fs.Read(buffer, 0, buffer.Length);
while(size > 0) {
for(int i = 0; i < hashes.Length; i++) {
hashes[i].TransformBlock(buffer, 0, size, buffer, 0);
}
size = fs.Read(buffer, 0, buffer.Length);
}
for(int i = 0; i < hashes.Length; i++) {
hashes[i].TransformFinalBlock(buffer, 0, 0);
Console.WriteLine(names[i] + BytesToHex(hashes[i].Hash));
hashes[i].Clear();
}
fs.Close();
}
示例4: GetKeyUri
/// <summary>Build a URI for secret key provisioning.</summary>
/// <param name="algorithm">The hashing algorithm for the HMAC computation.</param>
/// <param name="issuer">The name of the entity issuing and maintaining the key.</param>
/// <param name="account">The account name for which the one-time codes will work.</param>
/// <param name="secret">The ASCII-encoded base32-encoded shared secret.</param>
/// <param name="period">The period step for the HMAC counter computation.</param>
/// <param name="digits">The number of digits of the one-time codes.</param>
/// <returns>The provisioning URI.</returns>
public static string GetKeyUri(
HashAlgorithm algorithm,
string issuer,
string account,
byte[] secret,
int digits = Otp.DefaultDigits,
int period = Totp.DefaultPeriod)
{
Contract.Requires<ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
Contract.Requires<ArgumentOutOfRangeException>(algorithm != HashAlgorithm.Unknown);
Contract.Requires<ArgumentNullException>(issuer != null);
Contract.Requires<ArgumentOutOfRangeException>(!string.IsNullOrWhiteSpace(issuer));
Contract.Requires<ArgumentNullException>(account != null);
Contract.Requires<ArgumentOutOfRangeException>(!string.IsNullOrWhiteSpace(account));
Contract.Requires<ArgumentNullException>(secret != null);
Contract.Requires<ArgumentException>(secret.Length > 0);
Contract.Requires<ArgumentOutOfRangeException>(digits > 0);
Contract.Requires<ArgumentOutOfRangeException>(period > 0);
Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result<string>()));
return
Otp.GetKeyUri(
OtpType.Totp,
issuer,
account,
secret,
algorithm,
digits,
0,
period);
}
示例5: GetCode
internal static int GetCode(
HashAlgorithm algorithm,
string secret,
long counter,
int digits)
{
MacAlgorithmProvider algorithmProvider = MacAlgorithmProvider.OpenAlgorithm(algorithm.ToAlgorithmName());
var keyMaterial = CryptographicBuffer.ConvertStringToBinary(secret, BinaryStringEncoding.Utf8);
var key = algorithmProvider.CreateKey(keyMaterial);
var hash = CryptographicEngine.Sign(key, CounterToBytes(counter).AsBuffer());
byte[] hashArray = new byte[hash.Length];
CryptographicBuffer.CopyToByteArray(hash, out hashArray);
var hmac = hashArray.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);
}
示例6: 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);
}
示例7: GetCode
/// <summary>Compute the one-time code for the given parameters.</summary>
/// <param name="algorithm">The hashing algorithm for the HMAC computation.</param>
/// <param name="secret">The ASCII-encoded base32-encoded shared secret.</param>
/// <param name="datetime">The date with time for which the one-time code must be computed.</param>
/// <param name="digits">The number of digits of the one-time codes.</param>
/// <param name="period">The period step used for the HMAC counter computation.</param>
/// <returns>The one-time code for the given date.</returns>
public static int GetCode(
HashAlgorithm algorithm,
string secret,
DateTime datetime,
int digits = Otp.DefaultDigits,
int period = Totp.DefaultPeriod)
{
Contract.Requires<ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
Contract.Requires<ArgumentOutOfRangeException>(algorithm != HashAlgorithm.Unknown);
Contract.Requires<ArgumentNullException>(secret != null);
Contract.Requires<ArgumentNullException>(datetime != null);
Contract.Requires<ArgumentException>(Enum.IsDefined(typeof(DateTimeKind), datetime.Kind));
Contract.Requires<ArgumentException>(datetime.Kind != DateTimeKind.Unspecified);
Contract.Requires<ArgumentOutOfRangeException>(digits > 0);
Contract.Requires<ArgumentOutOfRangeException>(period > 0);
Contract.Ensures(Contract.Result<int>() > 0);
Contract.Ensures(Contract.Result<int>() < Math.Pow(10, digits));
datetime = datetime.Kind == DateTimeKind.Utc ? datetime : datetime.ToUniversalTime();
var unixTime = datetime.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
var counter = (long) (unixTime * 1000) / (period * 1000);
return Otp.GetCode(algorithm, secret, counter, digits);
}
示例8: WriteHash
public static void WriteHash(XmlNode node, HashAlgorithm hash, DocPosition docPos, AncestralNamespaceContextManager anc) {
if (node is ICanonicalizableNode) {
((ICanonicalizableNode) node).WriteHash(hash, docPos, anc);
} else {
WriteHashGenericNode(node, hash, docPos, anc);
}
}
示例9: VerifySignatureWithHashAlgorithm
private static void VerifySignatureWithHashAlgorithm(AsymmetricSignatureFormatter formatter, AsymmetricSignatureDeformatter deformatter, HashAlgorithm hashAlgorithm)
{
byte[] signature = formatter.CreateSignature(hashAlgorithm);
Assert.True(deformatter.VerifySignature(hashAlgorithm, signature));
signature[signature.Length - 1] ^= 0xff;
Assert.False(deformatter.VerifySignature(hashAlgorithm, signature));
}
示例10: ValidateHashAlgorithm
public static bool ValidateHashAlgorithm(HashAlgorithm value)
{
if ((((value != HashAlgorithm.None) && (value != HashAlgorithm.Md2)) && ((value != HashAlgorithm.Md4) && (value != HashAlgorithm.Md5))) && (value != HashAlgorithm.Sha))
{
return (value == HashAlgorithm.Mac);
}
return true;
}
示例11: Compute
public static void Compute(HashAlgorithm hashAlgorithm, IUVStream<ArraySegment<byte>> stream, Action<byte[]> callback)
{
var hs = new HashStream(hashAlgorithm, stream);
hs.Complete += () => {
callback(hs.Hash);
hs.Dispose();
};
}
示例12: GetDigestedBytes
internal byte[] GetDigestedBytes(HashAlgorithm hash) {
m_c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, m_ancMgr);
hash.TransformFinalBlock(new byte[0], 0, 0);
byte[] res = (byte[]) hash.Hash.Clone();
// reinitialize the hash so it is still usable after the call
hash.Initialize();
return res;
}
示例13: VerifyIncrementalHash
public static void VerifyIncrementalHash(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm)
{
using (referenceAlgorithm)
using (IncrementalHash incrementalHash = IncrementalHash.CreateHash(hashAlgorithm))
{
VerifyIncrementalResult(referenceAlgorithm, incrementalHash);
}
}
示例14: ComputeString
public static void ComputeString(HashAlgorithm hashAlgorithm, IUVStream stream, Action<string> callback)
{
var hs = new HashStream(hashAlgorithm, stream);
hs.Complete += () => {
callback(hs.HashString);
hs.Dispose();
};
}
示例15: 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();
}