unsignedBigIntToBytes function

Uint8List unsignedBigIntToBytes(
  1. BigInt value,
  2. int length
)

Converts an unsigned BigInt into big-endian bytes of length.

Implementation

Uint8List unsignedBigIntToBytes(BigInt value, int length) {
  final result = Uint8List(length);
  var v = value;
  for (var i = length - 1; i >= 0; i--) {
    result[i] = (v & BigInt.from(0xff)).toInt();
    v = v >> 8;
  }
  if (v != BigInt.zero) {
    throw CryptoException('Value does not fit in $length bytes');
  }
  return result;
}