signAndVerify static method

Future<String> signAndVerify(
  1. SimpleKeyPair keyPair,
  2. String message
)

The method is a static asynchronous method that performs signing and verification of a message using the Ed25519 digital signature algorithm The method takes a SimpleKeyPair object (keyPair) and a String (message) as input. An instance of the DartEd25519 class is created, configured with the Sha512 hashing algorithm for signature generation and verification. The signString method of the DartEd25519 instance is used to sign the message using the keyPair. This produces an unsignedSignature object. The verifyString method is then used to verify the signature by passing the message and the unsignedSignature to it. The result is stored in the isSignatureCorrect variable. If the signature is verified successfully (isSignatureCorrect is true), the following steps are performed: The signature bytes are converted to a base64 representation using the base64.encode function. The resulting base64 signature is then converted to a URL-safe base64 representation using the StringUtilities.convertFromBase64ToBase64UrlSafe method. The URL-safe base64 signature is returned as the result of the method. If the signature verification fails (isSignatureCorrect is false), an error is thrown using throw Error().

Implementation

static Future<String> signAndVerify(SimpleKeyPair keyPair, String message) async {
    final algorithm = DartEd25519(sha512: Sha512());

    // Signing the message
    Signature unsignedSignature = await algorithm.signString(
      message,
      keyPair: keyPair,
    );

    // Verifying the signature
    final isSignatureCorrect = await algorithm.verifyString(
      message,
      signature: unsignedSignature,
    );

    if (isSignatureCorrect) {
      // Converting the signature bytes to base64 representation
      var base64Sig = base64.encode(unsignedSignature.bytes);

      // Converting the base64 signature to a URL-safe base64 representation
      var base64UrlSafeSig = StringUtilities.convertFromBase64ToBase64UrlSafe(base64Sig);

      return base64UrlSafeSig;
    } else {
      // If the signature verification fails, an error is thrown
      throw Error();
    }
  }