bytesIncrementBigEndian function

void bytesIncrementBigEndian(
  1. Uint8List bytes,
  2. int n
)

Interprets the bytes a big endian integer and increments them by int.

This can be useful for incrementing a nonce.

Example

import 'package:cryptography_plus/helpers.dart';

void main() {
  final bytes = [0,2,255];
  bytesIncrementBigEndian(bytes, 5);
  // bytes become [0,3,4]
}

Implementation

void bytesIncrementBigEndian(Uint8List bytes, int n) {
  if (n < 0) {
    throw ArgumentError.value(n, 'n');
  }
  for (var i = bytes.length - 1; n != 0 && i >= 0; i--) {
    final tmp = bytes[i] + n;
    bytes[i] = 0xFF & tmp;

    // Carry
    n = tmp ~/ 0x100;
  }
}