deriveSecret function

Secret deriveSecret(
  1. dynamic sharedKey
)

Implementation

Secret deriveSecret(sharedKey) {
  if (!(sharedKey is Uint8List) && !(sharedKey is String)) {
    throw "'sharedKey' must be a string or Uint8List";
  }

  if (sharedKey is String) {
    if (isHex(sharedKey)) {
      sharedKey = hexToUint8List(sharedKey);
    } else {
      throw "'sharedKey' must be an hexadecimal string";
    }
  }

  crypto.Hmac hmac = new crypto.Hmac(crypto.sha256, Uint8List(0));
  crypto.Digest digest = hmac.convert(utf8.encode(sharedKey));
  Uint8List pseudoRandomKey = Uint8List.fromList(digest.bytes);

  hmac = new crypto.Hmac(crypto.sha256, pseudoRandomKey);
  digest = hmac.convert(utf8.encode("0"));
  Uint8List iv = Uint8List.fromList(digest.bytes.sublist(0, 32));

  hmac = new crypto.Hmac(crypto.sha256, iv);
  digest = hmac.convert(utf8.encode("1"));
  Uint8List aesKey = Uint8List.fromList(digest.bytes.sublist(0, 32));

  return Secret(iv: iv, aesKey: aesKey);
}