importRawKey static method

Future<HmacSecretKey> importRawKey(
  1. List<int> keyData,
  2. Hash hash, {
  3. int? length,
})

Import HmacSecretKey from raw keyData.

Creates an HmacSecretKey using keyData as secret key, and running HMAC with given hash algorithm.

If given, length specifies the length of the key, this must be not be less than number of bits in keyData - 7. The length only allows cutting bits of the last byte in keyData. In practice this is the same as zero'ing the last bits in keyData.

Example

import 'dart:convert' show utf8;
import 'package:webcrypto/webcrypto.dart';

final key = await HmacSecretKey.importRawKey(
  base64.decode('WzIxLDg0LDEwMCw5OSwxMCwxMDUsMjIsODAsMTkwLDExNiwyMDMsMjQ5XQ=='),
  Hash.sha256,
);

Implementation

static Future<HmacSecretKey> importRawKey(
  List<int> keyData,
  Hash hash, {
  int? length,
}) {
  // These limitations are given in Web Cryptography Spec:
  // https://www.w3.org/TR/WebCryptoAPI/#hmac-operations
  if (length != null && length > keyData.length * 8) {
    throw ArgumentError.value(
        length, 'length', 'must be less than number of bits in keyData');
  }
  if (length != null && length <= (keyData.length - 1) * 8) {
    throw ArgumentError.value(
      length,
      'length',
      'must be greater than number of bits in keyData - 8, you can attain '
          'the same effect by removing bytes from keyData',
    );
  }

  return impl.hmacSecretKey_importRawKey(keyData, hash, length: length);
}