createSignature method

String createSignature(
  1. String authId,
  2. String helloNonce,
  3. Extra extra,
  4. HashMap<String, Object?> authExtra,
)

Calculates the client proof according to the WAMP-SCRAM specs where authId is the username that has already been saslpreped with Saslprep.saslprep(input) and helloNonce is a randomly generated nonce according to the WAMP-SCRAM specs. The keylength is 32 according to the WAMP-SCRAM specs

Implementation

String createSignature(String authId, String helloNonce, Extra extra,
    HashMap<String, Object?> authExtra) {
  late Uint8List saltedPassword;
  if (extra.kdf == kdfPbkdf2) {
    saltedPassword = CraAuthentication.deriveKey(
        _secret!,
        extra.salt == null
            ? CraAuthentication.defaultKeySalt
            : base64.decode(extra.salt!),
        iterations: extra.iterations!,
        keylen: defaultKeyLength);
  } else if (extra.kdf == kdfArgon) {
    saltedPassword = Uint8List(32);
    Argon2BytesGenerator()
      ..init(Argon2Parameters(Argon2Parameters.ARGON2_id,
          Uint8List.fromList(base64.decode(extra.salt!)),
          desiredKeyLength: defaultKeyLength,
          iterations: extra.iterations ?? 1000,
          memory: extra.memory ?? 100,
          version: Argon2Parameters.ARGON2_VERSION_13))
      ..deriveKey(
          Uint8List.fromList(_secret!.codeUnits), 0, saltedPassword, 0);
  }

  var clientKey = CraAuthentication.encodeByteHmac(
      saltedPassword, defaultKeyLength, 'Client Key'.codeUnits);
  var storedKey = SHA256Digest().process(Uint8List.fromList(clientKey));
  var clientSignature = CraAuthentication.encodeByteHmac(
      storedKey,
      defaultKeyLength,
      createAuthMessage(authId, helloNonce, authExtra, extra).codeUnits);
  var signature = [
    for (int i = 0; i < clientKey.length; i++)
      clientKey[i] ^ clientSignature[i]
  ];
  return base64.encode(signature);
}