本文整理汇总了C#中System.Security.Cryptography.KeyedHashAlgorithm类的典型用法代码示例。如果您正苦于以下问题:C# KeyedHashAlgorithm类的具体用法?C# KeyedHashAlgorithm怎么用?C# KeyedHashAlgorithm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
KeyedHashAlgorithm类属于System.Security.Cryptography命名空间,在下文中一共展示了KeyedHashAlgorithm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HMACSign
public static string HMACSign(string data, SecureString key, KeyedHashAlgorithm algorithm)
{
string str;
if (key == null)
{
throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
}
if (string.IsNullOrEmpty(data))
{
throw new ArgumentNullException("data", "Please specify data to sign.");
}
if (algorithm == null)
{
throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
}
IntPtr zero = IntPtr.Zero;
char[] destination = new char[key.Length];
try
{
zero = Marshal.SecureStringToBSTR(key);
Marshal.Copy(zero, destination, 0, destination.Length);
algorithm.Key = Encoding.UTF8.GetBytes(destination);
str = Convert.ToBase64String(algorithm.ComputeHash(Encoding.UTF8.GetBytes(data.ToCharArray())));
}
finally
{
Marshal.ZeroFreeBSTR(zero);
algorithm.Clear();
Array.Clear(destination, 0, destination.Length);
}
return str;
}
示例2: ShortKeyedHashAlgorithm
public ShortKeyedHashAlgorithm (KeyedHashAlgorithm baseAlgo, int outputHashBits)
{
_base = baseAlgo;
_hashBits = outputHashBits;
if (outputHashBits <= 0 || _base.HashSize < outputHashBits)
throw new ArgumentOutOfRangeException ();
}
示例3: ComputeSignature
private void ComputeSignature(KeyedHashAlgorithm hash)
{
this.Signature.SignedInfo.ComputeReferenceDigests();
this.Signature.SignedInfo.ComputeHash(hash);
byte[] signatureValue = hash.Hash;
this.Signature.SetSignatureValue(signatureValue);
}
示例4: HmacInfo
public HmacInfo(KeyedHashAlgorithm algorithm, int keySize)
{
Contract.Requires(algorithm != null);
KeySize = keySize;
Hmac = key => new HmacAlgorithm(algorithm, keySize, key);
}
示例5: GenerateKey
ProtectedKey GenerateKey(KeyedHashAlgorithm algorithm, DataProtectionScope dataProtectionScope)
{
using (algorithm)
{
return ProtectedKey.CreateFromPlaintextKey(algorithm.Key, dataProtectionScope);
}
}
示例6: KeyHashAlgorithmGenerator
protected KeyHashAlgorithmGenerator(KeyedHashAlgorithm algorithm)
{
if (algorithm == null)
{
throw new ArgumentNullException("algorithm");
}
this._algorithm = algorithm;
}
示例7: Sign
public static string Sign(string data, SecureString key, KeyedHashAlgorithm algorithm)
{
if (key == null)
{
throw new AmazonCloudFrontException("The AWS Secret Access Key specified is NULL");
}
return AWSSDKUtils.HMACSign(data, key, algorithm);
}
示例8: HmacAlgorithm
public HmacAlgorithm(KeyedHashAlgorithm algorithm, int keySize, byte[] key)
{
Contract.Requires(algorithm != null);
Contract.Requires(key != null);
Contract.Requires(keySize == key.Length << 3);
_algorithm = algorithm;
algorithm.Key = key;
}
示例9: HMACSign
public static string HMACSign(string data, System.Security.SecureString key, KeyedHashAlgorithm algorithm)
#endif
{
#if UNITY
if (String.IsNullOrEmpty(key))
#else
if (null == key)
#endif
{
throw new ArgumentNullException("key", "The AWS Secret Access Key specified is NULL!");
}
if (String.IsNullOrEmpty(data))
{
throw new ArgumentNullException("data", "Please specify data to sign.");
}
if (null == algorithm)
{
throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
}
#if UNITY
try
{
algorithm.Key = Encoding.UTF8.GetBytes(key);
return Convert.ToBase64String(algorithm.ComputeHash(
Encoding.UTF8.GetBytes(data.ToCharArray()))
);
}
finally
{
algorithm.Clear();
}
#else
// pointer to hold unmanaged reference to SecureString instance
IntPtr bstr = IntPtr.Zero;
char[] charArray = new char[key.Length];
try
{
// Marshal SecureString into byte array
bstr = Marshal.SecureStringToBSTR(key);
Marshal.Copy(bstr, charArray, 0, charArray.Length);
algorithm.Key = Encoding.UTF8.GetBytes(charArray);
return Convert.ToBase64String(algorithm.ComputeHash(
Encoding.UTF8.GetBytes(data.ToCharArray()))
);
}
finally
{
// Make sure that the clear text data is zeroed out
Marshal.ZeroFreeBSTR(bstr);
algorithm.Clear();
Array.Clear(charArray, 0, charArray.Length);
}
#endif
}
示例10: SymmetricSignatureProvider
/// <summary>
/// Initializes a new instance of the <see cref="SymmetricSignatureProvider"/> class that uses an <see cref="SymmetricSecurityKey"/> to create and / or verify signatures over a array of bytes.
/// </summary>
/// <param name="key">The <see cref="SymmetricSecurityKey"/> used for signing.</param>
/// <param name="algorithm">The signature algorithm to use.</param>
/// <exception cref="ArgumentNullException">'key' is null.</exception>
/// <exception cref="ArgumentNullException">'algorithm' is null.</exception>
/// <exception cref="ArgumentException">'algorithm' contains only whitespace.</exception>
/// <exception cref="ArgumentOutOfRangeException">'<see cref="SymmetricSecurityKey"/>.KeySize' is smaller than <see cref="SignatureProviderFactory.MinimumSymmetricKeySizeInBits"/>.</exception>
/// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetKeyedHashAlgorithm"/> throws.</exception>
/// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetKeyedHashAlgorithm"/> returns null.</exception>
/// <exception cref="InvalidOperationException"><see cref="SymmetricSecurityKey.GetSymmetricKey"/> throws.</exception>
public SymmetricSignatureProvider(SymmetricSecurityKey key, string algorithm)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
if (null == algorithm)
{
throw new ArgumentNullException(algorithm);
}
if (string.IsNullOrWhiteSpace(algorithm))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10002, "algorithm"));
}
if (key.KeySize < SignatureProviderFactory.MinimumSymmetricKeySizeInBits)
{
throw new ArgumentOutOfRangeException("key.KeySize", key.KeySize, string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10603, key.GetType(), SignatureProviderFactory.MinimumSymmetricKeySizeInBits));
}
try
{
this.keyedHash = key.GetKeyedHashAlgorithm(algorithm);
}
catch (Exception ex)
{
if (DiagnosticUtility.IsFatal(ex))
{
throw;
}
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10632, algorithm, key, ex), ex);
}
if (this.keyedHash == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10633, algorithm, key));
}
try
{
this.keyedHash.Key = key.GetSymmetricKey();
}
catch (Exception ex)
{
if (DiagnosticUtility.IsFatal(ex))
{
throw;
}
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ErrorMessages.IDX10634, algorithm, key, ex), ex);
}
}
开发者ID:richardschneider,项目名称:azure-activedirectory-identitymodel-extensions-for-dotnet,代码行数:67,代码来源:SymmetricSignatureProvider.cs
示例11: ManagedPsha1
public ManagedPsha1(byte[] secret, byte[] label, byte[] seed)
{
this.secret = secret;
this.seed = DiagnosticUtility.Utility.AllocateByteArray(label.Length + seed.Length);
label.CopyTo(this.seed, 0);
seed.CopyTo(this.seed, label.Length);
this.aValue = this.seed;
this.chunk = new byte[0];
this.index = 0;
this.position = 0;
this.hmac = CryptoHelper.NewHmacSha1KeyedHashAlgorithm(secret);
this.buffer = DiagnosticUtility.Utility.AllocateByteArray((this.hmac.HashSize / 8) + this.seed.Length);
}
示例12: Pbkdf2
/// <summary>
/// Creates a new PBKDF2 stream.
/// </summary>
/// <param name="hmacAlgorithm">
/// The HMAC algorithm to use, for example <see cref="HMACSHA256"/>.
/// Make sure to set <see cref="KeyedHashAlgorithm.Key"/>.
/// </param>
/// <param name="salt">
/// The salt.
/// A unique salt means a unique PBKDF2 stream, even if the original key is identical.
/// </param>
/// <param name="iterations">The number of iterations to apply.</param>
public Pbkdf2(KeyedHashAlgorithm hmacAlgorithm, byte[] salt, int iterations)
{
Check.Null("hmacAlgorithm", hmacAlgorithm);
Check.Null("salt", salt);
Check.Length("salt", salt, 0, int.MaxValue - 4);
Check.Range("iterations", iterations, 1, int.MaxValue);
if (hmacAlgorithm.HashSize == 0 || hmacAlgorithm.HashSize % 8 != 0)
{ throw Exceptions.Argument("hmacAlgorithm", "Unsupported hash size."); }
int hmacLength = hmacAlgorithm.HashSize / 8;
_saltBuffer = new byte[salt.Length + 4]; Array.Copy(salt, _saltBuffer, salt.Length);
_iterations = iterations; _hmacAlgorithm = hmacAlgorithm;
_digest = new byte[hmacLength]; _digestT1 = new byte[hmacLength];
}
示例13: KeyedHashAlgorithmKeyCreator
/// <summary>
/// Initialize a new instance of the <see cref="KeyedHashAlgorithmKeyCreator"/> class.
/// </summary>
/// <param name="algorithmType">The <see cref="HashAlgorithm"/> type that should be used.</param>
public KeyedHashAlgorithmKeyCreator(Type algorithmType)
{
if (algorithmType == null)
{
throw new ArgumentNullException("algorithmType");
}
if (!typeof(HashAlgorithm).IsAssignableFrom(algorithmType))
{
throw new ArgumentException(Resources.TypeShouldDeriveFromHashAlgorithm, "algorithmType");
}
this.algorithm = CreateAlgorithm(algorithmType);
this.algorithmType = algorithmType;
}
示例14: KeyedHashAlgorithm
public KeyedHashAlgorithm(HashAlgorithmNames algorithm)
{
switch (algorithm)
{
case HashAlgorithmNames.Sha1:
alg = new C.HMACSHA1();
break;
case HashAlgorithmNames.Sha256:
alg = new C.HMACSHA256();
break;
case HashAlgorithmNames.Sha384:
alg = new C.HMACSHA384();
break;
case HashAlgorithmNames.Sha512:
alg = new C.HMACSHA512();
break;
case HashAlgorithmNames.Md5:
alg = new C.HMACMD5();
break;
default:
throw new NotSupportedException();
}
}
示例15: HMACSign
/// <summary>
/// Computes RFC 2104-compliant HMAC signature
/// </summary>
/// <param name="data">The data to be signed</param>
/// <param name="key">The secret signing key</param>
/// <param name="algorithm">The algorithm to sign the data with</param>
/// <exception cref="T:System.ArgumentNullException"/>
/// <returns>A string representing the HMAC signature</returns>
public static string HMACSign(byte[] data, string key, KeyedHashAlgorithm algorithm)
{
if (String.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
}
if (data == null || data.Length == 0)
{
throw new ArgumentNullException("data", "Please specify data to sign.");
}
if (null == algorithm)
{
throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
}
try
{
algorithm.Key = Encoding.UTF8.GetBytes(key);
byte[] bytes = algorithm.ComputeHash(data);
return Convert.ToBase64String(bytes);
}
finally
{
algorithm.Clear();
}
}