本文整理汇总了C#中ArraySegment.GetValueOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# ArraySegment.GetValueOrDefault方法的具体用法?C# ArraySegment.GetValueOrDefault怎么用?C# ArraySegment.GetValueOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ArraySegment
的用法示例。
在下文中一共展示了ArraySegment.GetValueOrDefault方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateBuffer
internal static byte[] CreateBuffer(ArraySegment<byte>? label, ArraySegment<byte>? context, uint keyLengthInBits)
{
int labelLength = (label != null) ? label.GetValueOrDefault().Count : 0;
int contextLength = (context != null) ? context.GetValueOrDefault().Count : 0;
int bufferLength = (COUNTER_LENGTH /* counter */) + (labelLength + 1 /* label + 0x00 */) + (contextLength /* context */) + (DERIVED_KEY_LENGTH_LENGTH /* [L]_2 */);
var buffer = new byte[bufferLength];
// store label, if any
if (labelLength > 0)
Utils.BlockCopy(label.GetValueOrDefault().Array, label.GetValueOrDefault().Offset, buffer, COUNTER_LENGTH, labelLength);
// store context, if any
if (contextLength > 0)
Utils.BlockCopy(context.GetValueOrDefault().Array, context.GetValueOrDefault().Offset, buffer, COUNTER_LENGTH + labelLength + 1, contextLength);
// store key length
new Utils.IntStruct { UintValue = keyLengthInBits }.ToBEBytes(buffer, bufferLength - DERIVED_KEY_LENGTH_LENGTH);
return buffer;
}// CreateBuffer()
示例2: Encrypt
}// Encrypt()
public static void Decrypt(byte[] masterKey, ArraySegment<byte> ciphertext, ref ArraySegment<byte>? outputSegment, ArraySegment<byte>? salt = null, uint counter = 1)
{
int cipherLength = ciphertext.Count - CONTEXT_BUFFER_LENGTH - MAC_LENGTH;
if (cipherLength < 0) { outputSegment = null; return; }
try
{
Kdf.SP800_108_Ctr.DeriveKey(hmacFactory: _hmacFactory, key: masterKey, label: salt, context: new ArraySegment<byte>(ciphertext.Array, ciphertext.Offset, CONTEXT_TWEAK_LENGTH), derivedOutput: new ArraySegment<byte>(_sessionKey.Value), counter: counter);
Utils.BlockCopy(_sessionKey.Value, 0, _macKey.Value, 0, MAC_KEY_LENGTH);
using (var hmac = _hmacFactory())
{
hmac.Key = _macKey.Value;
hmac.TransformBlock(ciphertext.Array, ciphertext.Offset + CONTEXT_TWEAK_LENGTH, NONCE_LENGTH + cipherLength, null, 0);
hmac.TransformFinalBlock(ciphertext.Array, 0, 0);
var fullmacActual = hmac.HashInner;
if (!Utils.ConstantTimeEqual(fullmacActual, 0, ciphertext.Array, ciphertext.Offset + ciphertext.Count - MAC_LENGTH, MAC_LENGTH)) { outputSegment = null; return; };
}// using hmac
if (outputSegment == null) outputSegment = (new byte[cipherLength]).AsNullableArraySegment();
Utils.BlockCopy(ciphertext.Array, ciphertext.Offset + CONTEXT_TWEAK_LENGTH, _counterBuffer.Value, 0, NONCE_LENGTH);
Utils.BlockCopy(_sessionKey.Value, MAC_KEY_LENGTH, _encKey.Value, 0, ENC_KEY_LENGTH);
using (var ctrTransform = new Cipher.AesCtrCryptoTransform(key: _encKey.Value, counterBufferSegment: _counterBuffer.Value.AsArraySegment(), aesFactory: _aesFactory))
{
ctrTransform.TransformBlock(inputBuffer: ciphertext.Array, inputOffset: ciphertext.Offset + CONTEXT_BUFFER_LENGTH, inputCount: cipherLength, outputBuffer: outputSegment.GetValueOrDefault().Array, outputOffset: outputSegment.GetValueOrDefault().Offset);
}// using aesDecryptor
}
finally { EtM_CTR.ClearKeyMaterial(); }
}// Decrypt()