다음을 통해 공유


HMACSHA256 Constructor (array<Byte[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the HMACSHA256 class with the specified key data.

Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub New ( _
    key As Byte() _
)
public HMACSHA256(
    byte[] key
)

Parameters

  • key
    Type: array<System.Byte[]
    The secret key for HMACSHA256 encryption. The key can be any length, but if it is more than 64 bytes long it will be hashed (using SHA-1) to derive a 64-byte key. Therefore, the recommended size of the secret key is 64 bytes.

Exceptions

Exception Condition
ArgumentNullException

The key parameter is nulla null reference (Nothing in Visual Basic).

Remarks

HMACSHA256 is a type of keyed hash algorithm that is constructed from the SHA-256 hash function and used as a Hash-based Message Authentication Code (HMAC). The HMAC process mixes a secret key with the message data, hashes the result with the hash function, mixes that hash value with the secret key again, and then applies the hash function a second time. The output hash is 256 bits in length.

Examples

The following example shows how to use the HMACSHA256 constructor to encrypt an isolated storage file. This code example is part of a larger example provided for the HMACSHA256 class.

' Initialize the keyed hash object.
Dim myhmacsha256 As New HMACSHA256(key)
Dim inStream As IsolatedStorageFileStream = isoStore.OpenFile(sourceFilePath, FileMode.Open)
inStream.Position = 0
' Compute the hash of the input file.
Dim hashValue As Byte() = myhmacsha256.ComputeHash(inStream)
' Reset inStream to the beginning of the file.
inStream.Position = 0
' Write the computed hash value to the output file.
outStream.Write(hashValue, 0, hashValue.Length)
' Copy the contents of the sourceFile to the destFile.
Dim bytesRead As Integer
' read 1K at a time
Dim buffer(1023) As Byte
Do
    ' Read from the wrapping CryptoStream.
    bytesRead = inStream.Read(buffer, 0, 1024)
    outStream.Write(buffer, 0, bytesRead)
Loop While bytesRead > 0
myhmacsha256.Clear()
// Initialize the keyed hash object.
HMACSHA256 myhmacsha256 = new HMACSHA256(key);
IsolatedStorageFileStream inStream = isoStore.OpenFile(sourceFilePath, FileMode.Open);
inStream.Position = 0;
// Compute the hash of the input file.
byte[] hashValue = myhmacsha256.ComputeHash(inStream);
// Reset inStream to the beginning of the file.
inStream.Position = 0;
// Write the computed hash value to the output file.
outStream.Write(hashValue, 0, hashValue.Length);
// Copy the contents of the sourceFile to the destFile.
int bytesRead;
// read 1K at a time
byte[] buffer = new byte[1024];
do
{
    // Read from the wrapping CryptoStream.
    bytesRead = inStream.Read(buffer, 0, 1024);
    outStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
myhmacsha256.Clear();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.