using System.Text; using NSec.Cryptography; namespace StabilityMatrix.Core.Updater; public class SignatureChecker { private static readonly SignatureAlgorithm Algorithm = SignatureAlgorithm.Ed25519; private const string UpdatePublicKey = "-----BEGIN PUBLIC KEY-----\n" + "MCowBQYDK2VwAyEAqYXhKG1b0iOMnAZGBSBdFlFEWpFBIbIPQk0TtyE2SfI=\n" + "-----END PUBLIC KEY-----\n"; private readonly PublicKey publicKey; /// /// Initializes a new instance of SignatureChecker. /// /// Pkix format public key. Defaults to update verification key. public SignatureChecker(string? publicKeyPkix = null) { publicKey = PublicKey.Import( Algorithm, Encoding.ASCII.GetBytes(publicKeyPkix ?? UpdatePublicKey), KeyBlobFormat.PkixPublicKeyText); } /// /// Verifies the signature of provided data. /// /// Data to verify /// Signature in base64 encoding /// True if verified public bool Verify(string data, string signature) { var signatureBytes = Convert.FromBase64String(signature); return Algorithm.Verify(publicKey, Encoding.UTF8.GetBytes(data), signatureBytes); } /// /// Verifies the signature of provided data. /// /// Data to verify /// Signature in base64 encoding /// True if verified public bool Verify(ReadOnlySpan data, ReadOnlySpan signature) { return Algorithm.Verify(publicKey, data, signature); } }