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";
    }
  }

  final Digest sha256 = Digest('SHA-256');
  final Uint8List pseudoRandomKey = sha256.process(sharedKey);

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

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

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