sign method

List<int> sign(
  1. List<int> data,
  2. HashFunc hashMethod
)

Signs the provided data using this private key.

Implementation

List<int> sign(List<int> data, HashFunc hashMethod) {
  List<int> dom = List.empty();
  if (generator.curve == Curves.curveEd448) {
    dom = List<int>.from([...'SigEd448'.codeUnits, 0x00, 0x00]);
  }
  final r = BigintUtils.fromBytes(
      hashMethod()
          .update(List<int>.from([...dom, ...extendedKey, ...data]))
          .digest(),
      byteOrder: Endian.little);
  final R = (generator * r).toBytes();
  BigInt k = BigintUtils.fromBytes(
      hashMethod()
          .update(
              List<int>.from([...dom, ...R, ...publicKey.toBytes(), ...data]))
          .digest(),
      byteOrder: Endian.little);

  k %= generator.order!;
  final s = (r + k * secret) % generator.order!;
  final signature = [
    ...R,
    ...BigintUtils.toBytes(s, length: baselen, order: Endian.little)
  ];
  if (publicKey.verify(data, signature, hashMethod)) {
    return signature;
  }
  throw const CryptoException(
      'The created signature does not pass verification.');
}