本文整理汇总了C#中System.Security.Cryptography.HMAC类的典型用法代码示例。如果您正苦于以下问题:C# HMAC类的具体用法?C# HMAC怎么用?C# HMAC使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HMAC类属于System.Security.Cryptography命名空间,在下文中一共展示了HMAC类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MerchantTerminal
public MerchantTerminal(string GatewayID, string TerminalPassword, string HMACKeyID, string HMACKey)
{
mGatewayID = GatewayID;
mTerminalPassword = TerminalPassword;
mHMACKeyID = HMACKeyID;
mHMACEncryptionClient = new HMACSHA1(Encoding.UTF8.GetBytes(HMACKey));
}
示例2: Compute_PHash
static byte[] Compute_PHash(int bytes, byte[][] seeds, HMAC hmac, int blockSize)
{
int blocks = (bytes / blockSize) + (bytes % blockSize == 0 ? 0 : 1);
byte[] ret = new byte[blockSize * blocks];
byte[] prev = null;
for (int i = 0; i < blocks; i++) {
hmac.Initialize ();
if (prev == null) {
for (int q = 0; q < seeds.Length; q ++)
hmac.TransformBlock (seeds[q], 0, seeds[q].Length, seeds[q], 0);
} else {
hmac.TransformBlock (prev, 0, prev.Length, prev, 0);
}
hmac.TransformFinalBlock (Utility.EmptyByteArray, 0, 0);
prev = hmac.Hash;
hmac.Initialize ();
hmac.TransformBlock (prev, 0, prev.Length, prev, 0);
for (int q = 0; q < seeds.Length; q++)
hmac.TransformBlock (seeds[q], 0, seeds[q].Length, seeds[q], 0);
hmac.TransformFinalBlock (Utility.EmptyByteArray, 0, 0);
for (int q = 0; q < blockSize; q++)
ret[i * blockSize + q] = hmac.Hash[q];
}
return ret;
}
示例3: DeriveKey
}// DeriveKey()
internal static void DeriveKey(HMAC keyedHmac, ArraySegment<byte> bufferSegment, ArraySegment<byte> derivedOutput, uint counter = 1)
{
int derivedOutputCount = derivedOutput.Count, derivedOutputOffset = derivedOutput.Offset;
byte[] K_i = null;
HMAC2 keyedHmac2 = keyedHmac as HMAC2;
checked
{
// Calculate each K_i value and copy the leftmost bits to the output buffer as appropriate.
for (var counterStruct = new Utils.IntStruct { UintValue = counter }; derivedOutputCount > 0; ++counterStruct.UintValue)
{
counterStruct.ToBEBytes(bufferSegment.Array, bufferSegment.Offset); // update the counter within the buffer
if (keyedHmac2 == null)
{
K_i = keyedHmac.ComputeHash(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count);
}
else
{
keyedHmac2.TransformBlock(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count, null, 0);
keyedHmac2.TransformFinalBlock(bufferSegment.Array, 0, 0);
K_i = keyedHmac2.HashInner;
}
// copy the leftmost bits of K_i into the output buffer
int numBytesToCopy = derivedOutputCount > K_i.Length ? K_i.Length : derivedOutputCount;//Math.Min(derivedOutputCount, K_i.Length);
Utils.BlockCopy(K_i, 0, derivedOutput.Array, derivedOutputOffset, numBytesToCopy);
derivedOutputOffset += numBytesToCopy;
derivedOutputCount -= numBytesToCopy;
}// for
}// checked
if (keyedHmac2 == null && K_i != null) Array.Clear(K_i, 0, K_i.Length); /* clean up needed only when HMAC implementation is not HMAC2 */
}// DeriveKey()
示例4: HMACBuildInAdapter
public HMACBuildInAdapter(HMAC a_hmac, int a_blockSize)
: base(a_hmac.HashSize / 8, a_blockSize)
{
Debug.Assert(a_hmac != null);
m_hmac = a_hmac;
}
示例5: NistSP800108DeriveBytes
/// <summary>
/// Initializes a new instance of the NistSP800108DeriveBytes using specified algorithm.
/// </summary>
/// <param name="masterKey">The master key to derive from.</param>
/// <param name="label">The primary purpose string.</param>
/// <param name="context">The secondary purpose strings.</param>
/// <param name="pseudoRandomFunction">The HMAC function to use as PRF.</param>
public NistSP800108DeriveBytes(byte[] masterKey, string label, string[] context, HMAC pseudoRandomFunction) {
// Validate arguments
if (masterKey == null) throw new ArgumentNullException("masterKey");
if (masterKey.Length == 0) throw new ArgumentException("The argument cannot be empty.", "masterKey");
if (label == null) throw new ArgumentNullException("label");
if (string.IsNullOrWhiteSpace(label)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "label");
if (pseudoRandomFunction == null) throw new ArgumentNullException("pseudoRandomFunction");
// Setup internal parameters
this.pseudoRandomFunction = pseudoRandomFunction;
this.pseudoRandomFunction.Key = masterKey;
// Convert label and context to byte arrays
var safeUtf8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
this.labelBytes = safeUtf8.GetBytes(label);
if(context== null || context.Length > 0) {
this.contextBytes = new byte[0];
}
else {
using (MemoryStream stream = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(stream, safeUtf8)) {
foreach (string item in context) {
if (string.IsNullOrWhiteSpace(item)) continue; // Skip empty context item
writer.Write(item);
}
this.contextBytes = stream.ToArray();
}
}
}
示例6: HKDF
public HKDF(Func<HMAC> hmacFactory, byte[] ikm, byte[] salt = null, byte[] context = null)
{
hmac = hmacFactory();
hashLength = hmac.OutputBlockSize;
hmac.Key = salt ?? new byte[hashLength];
hmac.Key = hmac.ComputeHash(ikm); // re-keying hmac with PRK
this.context = context;
Reset();
}
示例7: PBKDF2
}//ctor
/// <summary>
/// ctor
/// </summary>
/// <param name="password"></param>
/// <param name="salt"></param>
/// <param name="iterations"></param>
public PBKDF2(Func<HMAC> hmacFactory, byte[] password, byte[] salt, int iterations)
{
this.Salt = salt;
this.IterationCount = iterations;
this.hmac = hmacFactory();
this.hmac.Key = password;
this.BlockSize = hmac.HashSize / 8;
this.Initialize();
}//ctor
示例8: EnableReceiveCipher
public void EnableReceiveCipher(ICryptoTransform decryptor, HMAC recvHMAC)
{
_decryptor = decryptor;
_recvHMAC = recvHMAC;
if (_recordType == RecordState.PlainText)
_recordType = RecordState.CipherTextReceiveOnly;
else
_recordType = RecordState.CipherText;
}
示例9: HKDF
/// <summary>
/// Initializes a new instance of the <see cref="HKDF"/> class.
/// </summary>
/// <param name="hmac">The HMAC hash function to use.</param>
/// <param name="ikm">input keying material.</param>
/// <param name="salt">optional salt value (a non-secret random value); if not provided, it is set to a string of HMAC.HashSize/8 zeros.</param>
public HKDF(HMAC hmac, byte[] ikm, byte[] salt = null)
{
this.hmac = hmac;
this.hashLength = hmac.HashSize / 8;
// now we compute the PRK
hmac.Key = salt ?? new byte[this.hashLength];
this.prk = hmac.ComputeHash(ikm);
}
示例10: GetAlgorithmParameters
private static void GetAlgorithmParameters( string algorithm, byte[] key, out byte[] aes_key, out byte[] hmac_key, out HMAC hmac )
{
switch ( algorithm )
{
case Aes128CbcHmacSha256.AlgorithmName:
{
if ( ( key.Length << 3 ) < 256 )
throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length in bits {1} < 256", algorithm, key.Length << 3 ) );
hmac_key = new byte[128 >> 3];
aes_key = new byte[128 >> 3];
Array.Copy( key, hmac_key, 128 >> 3 );
Array.Copy( key, 128 >> 3, aes_key, 0, 128 >> 3 );
hmac = new HMACSHA256( hmac_key );
break;
}
case Aes192CbcHmacSha384.AlgorithmName:
{
if ( ( key.Length << 3 ) < 384 )
throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length in bits {1} < 384", algorithm, key.Length << 3 ) );
hmac_key = new byte[192 >> 3];
aes_key = new byte[192 >> 3];
Array.Copy( key, hmac_key, 192 >> 3 );
Array.Copy( key, 192 >> 3, aes_key, 0, 192 >> 3 );
hmac = new HMACSHA384( hmac_key );
break;
}
case Aes256CbcHmacSha512.AlgorithmName:
{
if ( ( key.Length << 3 ) < 512 )
throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length in bits {1} < 512", algorithm, key.Length << 3 ) );
hmac_key = new byte[256 >> 3];
aes_key = new byte[256 >> 3];
Array.Copy( key, hmac_key, 256 >> 3 );
Array.Copy( key, 256 >> 3, aes_key, 0, 256 >> 3 );
hmac = new HMACSHA512( hmac_key );
break;
}
default:
{
throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "Unsupported algorithm: {0}", algorithm ) );
}
}
}
示例11: PBKDF2
/// <summary>
/// Creates new instance.
/// </summary>
/// <param name="algorithm">HMAC algorithm to use.</param>
/// <param name="input">The input used to derive the key.</param>
/// <param name="salt">The key salt used to derive the key.</param>
/// <param name="iterations">The number of iterations for the operation.</param>
/// <exception cref="System.ArgumentNullException">Algorithm cannot be null - Password cannot be null. -or- Salt cannot be null.</exception>
public PBKDF2(HMAC algorithm, Byte[] input, Byte[] salt, int iterations) {
if (algorithm == null) { throw new ArgumentNullException("algorithm", "Algorithm cannot be null."); }
if (salt == null) { throw new ArgumentNullException("salt", "Salt cannot be null."); }
if (input == null) { throw new ArgumentNullException("input", "input cannot be null."); }
this.Algorithm = algorithm;
this.Algorithm.Key = input;
this.Salt = salt;
this.Iterations = iterations;
this.BlockSize = 16;// this.Algorithm.HashSize / 8;
this.BufferBytes = new byte[this.BlockSize];
}
示例12: MainForm
public MainForm()
{
InitializeComponent();
cmbProviders.Items.AddRange(cryptoServices);
openFileDialog1.FileOk += openFileDialog1_FileOk;
Load += MainForm_Load;
FormClosing += MainForm_FormClosing;
Application.ApplicationExit += Application_ApplicationExit;
m_macAlgorithm = new HMACMD5();
m_rsaHelper = new RSASignHelper();
}
示例13: init
public void init(byte[] key)
{
if (key.Length > BSIZE)
{
byte[] tmp = new byte[BSIZE];
Array.Copy(key, 0, tmp, 0, BSIZE);
key = tmp;
}
mac = new SSC.HMACSHA1(key);
cs = new SSC.CryptoStream(Stream.Null,mac,SSC.CryptoStreamMode.Write);
}
示例14: HKDF
public HKDF(Func<HMAC> hmacFactory, byte[] ikm, byte[] salt = null, byte[] context = null)
{
hmac = hmacFactory();
hashLength = hmac.OutputBlockSize;
// a malicious implementation of HMAC could conceivably mess up the shared static empty byte arrays, which are still writeable...
hmac.Key = salt ?? (hashLength == 64 ? emptyArray64 : hashLength == 48 ? emptyArray48 : hashLength == 32 ? emptyArray32 : hashLength == 20 ? emptyArray20 : new byte[hashLength]);
hmac.Key = hmac.ComputeHash(ikm); // re-keying hmac with PRK
this.context = context;
Reset();
}
示例15: init
public void init(byte[] key)
{
if (key.Length > 20)
{
byte[] tmp = new byte[20];
Array.Copy(key, 0, tmp, 0, 20);
key = tmp;
}
mentalis_mac = new System.Security.Cryptography.HMACSHA1(key);
cs = new CryptoStream(Stream.Null, mentalis_mac, CryptoStreamMode.Write);
}