This documentation is archived and is not being maintained.


Computes a Hash-based Message Authentication Code (HMAC) using the SHA1 hash function.

For a list of all members of this type, see HMACSHA1 Members.


[Visual Basic]
Public Class HMACSHA1
   Inherits KeyedHashAlgorithm
public class HMACSHA1 : KeyedHashAlgorithm
public __gc class HMACSHA1 : public KeyedHashAlgorithm
public class HMACSHA1 extends KeyedHashAlgorithm

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


An HMAC can be used to determine whether a message sent over an insecure channel has been tampered with, provided that the sender and receiver share a secret key. The sender computes the HMAC for the original data and sends both the original data and HMAC as a single message. The receiver recomputes the HMAC on the received message and checks that the computed HMAC matches the transmitted HMAC.

Any change to the data or the HMAC will result in a mismatch, because knowledge of the secret key is required to change the message and reproduce the correct HMAC. Therefore, if the codes match, the message is authenticated.

HMACSHA1 accepts keys of any size, and produces a hash sequence of length 20 bytes.


[Visual Basic, C#, C++] The following example computes the HMAC for data using the SHA1 hash algorithm and stores it in result. This example assumes that there are predefined constants DATA_SIZE and KEY_SIZE.

[Visual Basic] 
Dim key(KEY_SIZE) As Byte
Dim data(DATA_SIZE) As Byte
Dim hmac As New HMACSHA1(key)
Dim cs As New CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write)
cs.Write(data, 0, data.Length)
Dim result As Byte() = hmac.Hash

byte[] key = new byte[KEY_SIZE];
byte[] data = new byte[DATA_SIZE];

HMACSHA1 hmac = new HMACSHA1(key);
CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);

byte[] result = hmac.Hash;

Byte key[] = new Byte[KEY_SIZE];
Byte data[] = new Byte[DATA_SIZE];

HMACSHA1* hmac = new HMACSHA1(key);
CryptoStream* cs = new CryptoStream(Stream::Null, hmac, CryptoStreamMode::Write);
cs->Write(data, 0, data->Length);

Byte result[] = hmac->Hash;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Security.Cryptography

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

HMACSHA1 Members | System.Security.Cryptography Namespace | Cryptographic Services