crypto_secretbox_open static method

Uint8List crypto_secretbox_open(
  1. Uint8List m,
  2. Uint8List c,
  3. int d,
  4. Uint8List n,
  5. Uint8List k,
)

Implementation

static Uint8List crypto_secretbox_open(
    Uint8List m, Uint8List c, final int d, Uint8List n, Uint8List k) {
  final x = Uint8List(32);
  if (d < 32) {
    throw 'The encrypted message must be at least 32-byte long';
  }

  crypto_stream(x, 0, 32, n, k);

  // NOTE: the hash offset is zero instead of 16
  if (_crypto_onetimeauth_verify(c, 16, c, 32, d - 32, x) != 0) {
    throw 'The message is forged or malformed or the shared secret is invalid';
    //return -1;
  }
  crypto_stream_xor(m, 0, c, 0, d, n, k);

  ///for (i = 0; i < 32; i++) m[i] = 0;
  // FIXME: Check in tweetnacl where these 32 bytes disappear?
  return m.sublist(32);
}