unpadBlock method

Iterable<int> unpadBlock(
  1. int blockSize,
  2. Iterable<int> block, {
  3. List<int>? labelHash,
  4. dynamic label,
})
override

Implementation

Iterable<int> unpadBlock(int blockSize, Iterable<int> block,
    {List<int>? labelHash, /* String | List<int> */ label}) {
  if (block.length != blockSize) {
    throw Exception('Invalid blocksize');
  }

  if (block.elementAt(0) != 0) {
    throw Exception('Invalid block. First byte not 0');
  }

  if (labelHash == null) {
    if (label == null) label = <int>[];

    labelHash = hasher.convert(label).bytes;
  }

  final maskedSeed = block.skip(1).take(labelHash.length).toList();
  final maskedDb = block.skip(1 + labelHash.length).toList();

  final seedMask = mgf.encode(labelHash.length, maskedDb);
  final seed = List<int>.generate(
      labelHash.length, (i) => seedMask[i] ^ maskedSeed[i]);

  final dbMask = mgf.encode(blockSize - labelHash.length - 1, seed);
  Iterable<int> db = List<int>.generate(
      blockSize - labelHash.length - 1, (i) => dbMask[i] ^ maskedDb[i]);

  final labelHashDash = db.take(labelHash.length);
  if (!iterableEquality.equals(labelHash, labelHashDash)) {
    throw Exception('Invalid block. Label hashes do not match');
  }
  db = db.skip(labelHash.length);

  db = db.skipWhile((value) => value == 0);

  if (db.isEmpty) {
    throw Exception('Invalid block. No delimiter');
  }

  if (db.first != 0x01) {
    throw Exception('Invalid block. Invalid delimiter');
  }

  return db.skip(1);
}