Home Reference Source Test
import Hmac from 'crypto-api/src/mac/hmac.mjs'
public class | source

Hmac

Calculates HMAC

Example:

Calculates HMAC-MD5 from string "message" with key "key" - ES6 style
import Md5 from "crypto-api/src/hasher/md5";
import Hmac from "crypto-api/src/mac/hmac";
import {toHex} from "crypto-api/src/encoder/hex";

let hasher = new Md5();
let hmac = new Hmac('key', hasher);
hmac.update('message');
console.log(toHex(hmac.finalize()));
Calculates HMAC-MD5 from UTF string "message" with UTF key "key" - ES6 style
import Md5 from "crypto-api/src/hasher/md5";
import Hmac from "crypto-api/src/mac/hmac";
import {toHex} from "crypto-api/src/encoder/hex";
import {fromUtf} from "crypto-api/src/encoder/utf";

let hasher = new Md5();
let hmac = new Hmac(fromUtf('key'), hasher);
hmac.update(fromUtf('message'));
console.log(toHex(hmac.finalize()));
Calculates HMAC-MD5 from string "message" with key "key" - ES5 style
<script src="https://nf404.github.io/crypto-api/crypto-api.min.js"></script>
<script>
  var hasher = CryptoApi.getHasher('md5');
  var hmac = CryptoApi.getHmac('key', hasher);
  hmac.update('message');
  console.log(CryptoApi.encoder.toHex(hmac.finalize()));
</script>
Calculates HMAC-MD5 from UTF string "message" with UTF key "key" - ES5 style
<script src="https://nf404.github.io/crypto-api/crypto-api.min.js"></script>
<script>
  var hasher = CryptoApi.getHasher('md5');
  console.log(CryptoApi.hmac('key', 'message', hasher));
</script>

Test:

Constructor Summary

Public Constructor
public

constructor(key: string, hasher: Hasher)

Method Summary

Public Methods
public

finalize(): string

Finalize hmac and return result

public

update(message: string)

Update message from binary string

Public Constructors

public constructor(key: string, hasher: Hasher) source

Params:

NameTypeAttributeDescription
key string
hasher Hasher

Public Methods

public finalize(): string source

Finalize hmac and return result

Return:

string

public update(message: string) source

Update message from binary string

Params:

NameTypeAttributeDescription
message string