本文整理汇总了C#中System.Security.Cryptography.SHA512Managed.TransformFinalBlock方法的典型用法代码示例。如果您正苦于以下问题:C# SHA512Managed.TransformFinalBlock方法的具体用法?C# SHA512Managed.TransformFinalBlock怎么用?C# SHA512Managed.TransformFinalBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SHA512Managed
的用法示例。
在下文中一共展示了SHA512Managed.TransformFinalBlock方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateIV
/// <summary>
/// Given a size in bytes, generate an initialization vector (IV) for encryption
/// </summary>
/// <param name="size">Size of the output byte array</param>
/// <returns>An array of bytes to be used as the IV</returns>
private static byte[] GenerateIV(int size, string key)
{
// Use SHA-512 to generate a pseudo-random IV. We'll use the user's username and
// the machine name as the key data; this isn't really super secure, but it should
// at least be unique per user/machine. Note that we're using the system default
// encoding here, rather than UTF-8; this is a bit of legacy code, and we can't
// really change this without breaking existing parameters. Since this is an
// internal thing, so it shouldn't be a problem.
SHA512Managed hasher = new SHA512Managed();
byte[] iv = Encoding.Default.GetBytes(key + Environment.UserName +
Environment.MachineName);
hasher.TransformFinalBlock(iv, 0, iv.Length);
iv = hasher.Hash;
// SHA-512 is probably overkill for the IV we need. So given the size value,
// chop of only the number of bytes we need and return those.
if (iv.Length > size)
{
byte[] slice = new byte[size];
Array.Copy(iv, 0, slice, 0, size);
return slice;
}
else return iv;
}
示例2: GetHmacKey64
internal static byte[] GetHmacKey64(byte[] pbKey, ulong uBlockIndex)
{
if(pbKey == null) throw new ArgumentNullException("pbKey");
Debug.Assert(pbKey.Length == 64);
// We are computing the HMAC using SHA-256, whose internal
// block size is 512 bits; thus create a key that is 512
// bits long (using SHA-512)
byte[] pbBlockKey;
using(SHA512Managed h = new SHA512Managed())
{
byte[] pbIndex = MemUtil.UInt64ToBytes(uBlockIndex);
h.TransformBlock(pbIndex, 0, pbIndex.Length, pbIndex, 0);
h.TransformBlock(pbKey, 0, pbKey.Length, pbKey, 0);
h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);
pbBlockKey = h.Hash;
}
#if DEBUG
byte[] pbZero = new byte[64];
Debug.Assert((pbBlockKey.Length == 64) && !MemUtil.ArraysEqual(
pbBlockKey, pbZero)); // Ensure we own pbBlockKey
#endif
return pbBlockKey;
}
示例3: GenerateKeyFromSite
/// <summary>
/// Given a string for a site name, generate the registry subkey name
/// </summary>
/// <param name="site">The site name to get the key from</param>
/// <returns>The generated registry key string, or null on failure</returns>
public static string GenerateKeyFromSite(string site)
{
try
{
// Otherwise, only bother if we have a site to work with:
if (!String.IsNullOrEmpty(site) && site.Length > 0)
{
// Get the SHA-512 of the site name. We'll also include the user's
// username and the machine name as a salt. We really should change
// the encoding here to something like UTF-8, but we'll leave this
// at the default to keep from breaking the user's data.
SHA512Managed hasher = new SHA512Managed();
byte[] keyBytes = Encoding.Default.GetBytes(site + Environment.UserName +
Environment.MachineName);
keyBytes = hasher.TransformFinalBlock(keyBytes, 0, keyBytes.Length);
// Convert the binary hash to Base64, then chop it down to 255 characters
// if it's too long. Windows registry keys can only be 255 characters
// in length.
string key = Convert.ToBase64String(keyBytes);
if (key.Length > 255) return key.Substring(0, 255);
else return key;
}
else return null;
}
catch { return null; }
}
示例4: Sha512
/// <summary>
/// Hash a string using the SHA512 algorithm. 128 unicode chars, 256 bytes
/// </summary>
public static string Sha512(this string plainMessage)
{
byte[] data = Encoding.UTF8.GetBytes(plainMessage);
using (HashAlgorithm sha = new SHA512Managed())
{
sha.TransformFinalBlock(data, 0, data.Length);
var sb = new StringBuilder();
foreach (byte bit in sha.Hash)
{
sb.Append(bit.ToString("x2"));
}
return sb.ToString();
}
}