本文整理汇总了C#中HashType.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# HashType.ToString方法的具体用法?C# HashType.ToString怎么用?C# HashType.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashType
的用法示例。
在下文中一共展示了HashType.ToString方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
public static HashAlgorithm Create(HashType hash)
{
switch (hash)
{
case HashType.CRC32:
return CRC32.Create();
case HashType.MD5:
return MD5.Create();
case HashType.SHA256:
return SHA256.Create();
case HashType.SHA384:
return SHA384.Create();
case HashType.SHA512:
return SHA512.Create();
case HashType.SHA1:
return SHA1.Create();
default:
throw new NotSupportedException("not support hash algorithm :" + hash.ToString());
}
}
示例2: ComputeHash
/// <summary>
/// Generates a hash for the given plain text value and returns a
/// base64-encoded result. Before the hash is computed, a random salt
/// is generated and appended to the plain text. This salt is stored at
/// the end of the hash value, so it can be used later for hash
/// verification.
/// </summary>
/// <param name="plainText">
/// Plaintext value to be hashed. The function does not check whether
/// this parameter is null.
/// </param>
/// <param name="hashAlgorithm">
/// Name of the hash algorithm. Allowed values are: "MD5", "SHA1",
/// "SHA256", "SHA384", and "SHA512" (if any other value is specified
/// MD5 hashing algorithm will be used). This value is case-insensitive.
/// </param>
/// <returns>
/// Hash value formatted as a base64-encoded string.
/// </returns>
public static string ComputeHash(string plainText,
HashType hashType)
{
// Convert plain text into a byte array.
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
// Allocate array, which will hold plain text.
var plainTextWithSaltBytes =
new byte[plainTextBytes.Length];
// Copy plain text bytes into resulting array.
for (int i = 0; i < plainTextBytes.Length; i++)
plainTextWithSaltBytes[i] = plainTextBytes[i];
// Because we support multiple hashing algorithms, we must define
// hash object as a common (abstract) base class. We will specify the
// actual hashing algorithm class later during object creation.
HashAlgorithm hash;
// Initialize appropriate hashing algorithm class.
switch (hashType.ToString())
{
case "SHA1":
hash = new SHA1Managed();
break;
case "SHA256":
hash = new SHA256Managed();
break;
case "SHA384":
hash = new SHA384Managed();
break;
case "SHA512":
hash = new SHA512Managed();
break;
default:
hash = new MD5CryptoServiceProvider();
break;
}
// Compute hash value of our plain text with appended salt.
var hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
// Create array which will hold hash and original salt bytes.
var hashWithSaltBytes = new byte[hashBytes.Length];
// Copy hash bytes into resulting array.
for (int i = 0; i < hashBytes.Length; i++)
hashWithSaltBytes[i] = hashBytes[i];
// Convert result into a base64-encoded string.
string hashValue = Convert.ToBase64String(hashWithSaltBytes);
// Return the result.
return hashValue;
}
示例3: Crypto
/// <summary>
///
/// </summary>
/// <param name="hType">Hashing Algorithm used (SHA1 or MD5)</param>
/// <param name="keyLength">Size of encryption key in bits. Allowed values are: 128, 192, and 256.</param>
/// <param name="iterations">Number of iterations used to generate password.</param>
/// <param name="initVector">16 ASCII character Initialization Vector</param>
/// <param name="passPhrase">Unicode phrase used to generate encryption string</param>
/// <param name="saltValue">Unicode value to salt data with (secondary key)</param>
public Crypto(HashType hType, int keyLength, int iterations, string initVector, string passPhrase, string saltValue)
{
m_hType = hType.ToString();
m_keyLength = keyLength;
m_iterations = iterations;
m_initVector = initVector;
m_passPhrase = passPhrase;
m_saltValue = saltValue;
}
示例4: CreatePasswordHash
/// <summary>
/// Creates the password hash using a given HashType algoritm.
/// </summary>
/// <param name="salt">The salt.</param>
/// <param name="password">The password.</param>
/// <param name="hashType">Type of the hash.</param>
/// <returns></returns>
public static string CreatePasswordHash(string salt, string password, HashType hashType)
{
string hashString = string.Empty;
if (!string.IsNullOrEmpty(password))
{
HashAlgorithm hashAlg = HashAlgorithm.Create(hashType.ToString());
byte[] pwordData = Encoding.Default.GetBytes(salt + password);
byte[] hash = hashAlg.ComputeHash(pwordData);
hashString = Convert.ToBase64String(hash);
}
return hashString;
}
示例5: CreateInstance
public static HashAlgorithm CreateInstance(HashType theHash)
{
HashAlgorithm hash = null;
switch(theHash)
{
case HashType.MD5:
{
hash = new MD5CryptoServiceProvider();
break;
}
case HashType.RIPEMD160:
{
hash = new RIPEMD160Managed();
break;
}
case HashType.SHA1:
{
hash = new SHA1CryptoServiceProvider();
break;
}
case HashType.SHA256:
{
hash = new SHA256CryptoServiceProvider();
break;
}
case HashType.SHA384:
{
hash = new SHA384CryptoServiceProvider();
break;
}
case HashType.SHA512:
{
hash = new SHA512CryptoServiceProvider();
break;
}
default:
{
throw new NotSupportedException(String.Format("The hash algorithm '{0}' is not supported", theHash.ToString()));
}
}
return hash;
}
示例6: CreateNative
private static HashAlgorithm CreateNative(HashType hashType)
{
switch(hashType)
{
case HashType.Md5:
return MD5.Create();
case HashType.Sha1:
return SHA1.Create();
case HashType.Sha256:
return SHA256.Create();
case HashType.Sha384:
return SHA384.Create();
case HashType.Sha512:
return SHA512.Create();
#if NETFULL
case HashType.RipeMd160:
return RIPEMD160.Create();
#endif
default:
throw new NotSupportedException(hashType.ToString() + " is not supported");
}
}
示例7: RunHash
private static void RunHash(HashType hashtype)
{
Console.WriteLine("Hash type: " + hashtype.ToString());
//Random r = new Random();
DateTime starttime = DateTime.Now;
//ConcurrentBag<String> SmallBag = new ConcurrentBag<String>();
//ConcurrentBag<String> MediumBag = new ConcurrentBag<String>();
//ConcurrentBag<String> LargeBag = new ConcurrentBag<String>();
//ConcurrentBag<String> GiantBag = new ConcurrentBag<String>();
Hasher hasher = new Hasher();
#region slow also
//ConcurrentBag<Int32> smallarrdisty = new ConcurrentBag<Int32>();
//ConcurrentBag<Int32> mediumarrdisty = new ConcurrentBag<Int32>();
//ConcurrentBag<Int32> largearrdisty = new ConcurrentBag<Int32>();
//ConcurrentBag<Int32> giantarrdisty = new ConcurrentBag<Int32>();
//Int32 smallarraycollisions = 0;
//Int32 mediumarraycollisions = 0;
//Int32 largearraycollisions = 0;
//Int32 giantarraycollisions = 0;
//IEnumerable<Hash> smallhash = null;
//IEnumerable<Hash> mediumhash = null;
//IEnumerable<Hash> largehash = null;
//IEnumerable<Hash> gianthash = null;
//Parallel.Invoke(
// () => smallhash = hasher.Hash(SmallBag, hashtype),
// () => mediumhash = hasher.Hash(MediumBag, hashtype),
// () => largehash = hasher.Hash(LargeBag, hashtype),
// () => gianthash = hasher.Hash(GiantBag, hashtype)
// );
//Parallel.Invoke(() =>
// Parallel.ForEach(smallhash, h =>
// {
// if (smallarrdisty.Contains(h.HashCode))
// smallarraycollisions++;
// else
// smallarrdisty.Add(h.HashCode);
// }), () =>
// Parallel.ForEach(mediumhash, h =>
// {
// if (mediumarrdisty.Contains(h.HashCode))
// mediumarraycollisions++;
// else
// mediumarrdisty.Add(h.HashCode);
// }), () =>
// Parallel.ForEach(largehash, h =>
// {
// if (largearrdisty.Contains(h.HashCode))
// largearraycollisions++;
// else
// largearrdisty.Add(h.HashCode);
// }), () =>
// Parallel.ForEach(gianthash, h =>
// {
// if (giantarrdisty.Contains(h.HashCode))
// giantarraycollisions++;
// else
// giantarrdisty.Add(h.HashCode);
// }));
////() =>
////{
//// hasher.Hash(MediumBag, hashtype).Map(h =>
//// {
//// if (mediumarrdisty.Contains(h.HashCode))
//// mediumarraycollisions++;
//// else
//// mediumarrdisty.Add(h.HashCode);
//// });
////}, () =>
////{
//// hasher.Hash(LargeBag, hashtype).Map(h =>
//// {
//// if (largearrdisty.Contains(h.HashCode))
//// largearraycollisions++;
//// else
//// largearrdisty.Add(h.HashCode);
//// });
////}, () =>
////{
//// hasher.Hash(GiantBag, hashtype).Map(h =>
//// {
//// if (giantarrdisty.Contains(h.HashCode))
//// giantarraycollisions++;
//// else
//// giantarrdisty.Add(h.HashCode);
//// });
////});
#endregion
#region slow way
//List<Int32> smallarrdisty = new List<Int32>(SmallArray.Length);
//Int32 smallarraycollisions = 0;
//hasher.Hash(SmallArray, hashtype).Map(h =>
// {
//.........这里部分代码省略.........
示例8: VerifyHash
/// <summary>
/// Compares a hash of the specified plain text value to a given hash
/// value. Plain text is hashed with the same salt value as the original
/// hash.
/// </summary>
/// <param name="plainText">
/// Plain text to be verified against the specified hash. The function
/// does not check whether this parameter is null.
/// </param>
/// <param name="hashAlgorithm">
/// Name of the hash algorithm. Allowed values are: "MD5", "SHA1",
/// "SHA256", "SHA384", and "SHA512" (if any other value is specified,
/// MD5 hashing algorithm will be used). This value is case-insensitive.
/// </param>
/// <param name="hashValue">
/// Base64-encoded hash value produced by ComputeHash function. This value
/// includes the original salt appended to it.
/// </param>
/// <returns>
/// If computed hash mathes the specified hash the function the return
/// value is true; otherwise, the function returns false.
/// </returns>
public static bool VerifyHash(string plainText,
HashType hashType,
string hashValue)
{
// Convert base64-encoded hash value into a byte array.
var hashWithSaltBytes = Convert.FromBase64String(hashValue);
// We must know size of hash (without salt).
int hashSizeInBits, hashSizeInBytes;
// Size of hash is based on the specified algorithm.
switch (hashType.ToString())
{
case "SHA1":
hashSizeInBits = 160;
break;
case "SHA256":
hashSizeInBits = 256;
break;
case "SHA384":
hashSizeInBits = 384;
break;
case "SHA512":
hashSizeInBits = 512;
break;
default: // Must be MD5
hashSizeInBits = 128;
break;
}
// Convert size of hash from bits to bytes.
hashSizeInBytes = hashSizeInBits/8;
// Make sure that the specified hash value is long enough.
if (hashWithSaltBytes.Length < hashSizeInBytes)
return false;
// Allocate array to hold original salt bytes retrieved from hash.
var saltBytes = new byte[hashWithSaltBytes.Length -
hashSizeInBytes];
// Copy salt from the end of the hash to the new array.
for (int i = 0; i < saltBytes.Length; i++)
saltBytes[i] = hashWithSaltBytes[hashSizeInBytes + i];
// Compute a new hash string.
string expectedHashString =
ComputeHash(plainText, hashType);
// If the computed hash matches the specified hash,
// the plain text value must be correct.
return (hashValue == expectedHashString);
}
示例9: AddJob
public String AddJob (byte[] hash, HashType type, int maxLength)
{
var localId = Guid.NewGuid ().ToString ();
var localJob = new LocalJob ();
var starters = Enumerable.Range (ASCII_0, ASCII_z - ASCII_0 + 1).ToList ();
lock (LocalJobs) {
LocalJobs.Add (localId, localJob);
int i = 0;
foreach (var rs in Sockets.Values) {
var request = new NetMQMessage ();
request.Append ("job");
request.Append (hash);
request.Append (type.ToString ());
request.Append (maxLength);
for (int a = 0; a < starters.Count; a++) {
if (a % Sockets.Count == i) {
request.Append (Encoding.ASCII.GetString (new byte[] { (byte)starters [a] }));
}
}
rs.SendMessage (request);
var recv = rs.ReceiveMessage ();
int id = recv [0].ConvertToInt32 ();
RemoteIdToLocal.Add (id, localId);
localJob.Nodes.Add (rs, id);
i++;
}
localJob.Status = Common.Status.PROCESSING;
}
return localId;
}
示例10: Format
public static string Format(HashType type, string input)
{
return $"{{{type.ToString()}:{input}}}";
}