deriveKey method

Future<List<int>> deriveKey(
  1. int length, {
  2. Uint8List? salt,
})

Derives a new key with the given length and optional salt.

The length parameter is the length of the derived key. The salt parameter is the optional salt to use.

Implementation

Future<List<int>> deriveKey(int length, {Uint8List? salt}) async {
  Hkdf hkdf = Hkdf(hmac: Hmac.sha256(), outputLength: length);
  SecretKeyData secretData = await hkdf.deriveKey(
      secretKey: secret,
      nonce: salt == null || salt.isEmpty
          ? Uint8List(Hmac.sha256().macLength)
          : salt.toList());
  return secretData.bytes;
}