public void CanValidateSignature() { Ed25519KeyPairGenerator kpg = new Ed25519KeyPairGenerator(); kpg.Init(new Ed25519KeyGenerationParameters(Random)); AsymmetricCipherKeyPair kp = kpg.GenerateKeyPair(); Ed25519PrivateKeyParameters privateKey = (Ed25519PrivateKeyParameters)kp.Private; Ed25519PublicKeyParameters publicKey = (Ed25519PublicKeyParameters)kp.Public; var pubKeyBase64 = Convert.ToBase64String(publicKey.GetEncoded()); // create signature for item byte[] msg = new byte[Random.NextInt() & 255]; Random.NextBytes(msg); var signer = new Ed25519Signer(); signer.Init(true, privateKey); signer.BlockUpdate(msg, 0, msg.Length); byte[] signature = signer.GenerateSignature(); var signatureForAppCast = Convert.ToBase64String(signature); // verify signature var checker = new Ed25519Checker(NetSparkleUpdater.Enums.SecurityMode.Strict, pubKeyBase64); Assert.True(checker.VerifySignature(signatureForAppCast, msg) == NetSparkleUpdater.Enums.ValidationResult.Valid); }
/// <summary> /// Signs the passed in data with a private key /// </summary> /// <param name="privateKey">the private key used to create the signature</param> /// <param name="data">The data to sign</param> /// <returns>the signature as a byte array</returns> public byte[] Sign(byte[] privateKey, byte[] data) { Ed25519PrivateKeyParameters privKey = null; try { privKey = (Ed25519PrivateKeyParameters)CreateAsymmetricKeyParameterFromPrivateKeyInfo(privateKey); } catch (InvalidCastException exception) { string message = "Private Key Import Failed!\n" + $"{exception.Message}.\n" + "The contents of the source do not represent a valid Ed25519 key parameter\n" + "Verify that the key is not corrupted.\n" + "- or - Verify that the correct key is selected."; throw new CryptoException(message, exception); } var signer = new Ed25519Signer(); signer.Init(true, privKey); signer.BlockUpdate(data, 0, data.Length); var signature = signer.GenerateSignature(); return(signature); }
byte[] ICryptoLibrary.SignEd25519(byte[] plainText, byte[] privateKey) { var signer = new Ed25519Signer(); signer.Init(true, new Ed25519PrivateKeyParameters(privateKey, 0)); signer.BlockUpdate(plainText, 0, plainText.Length); return(signer.GenerateSignature()); }
public bool verify(byte[] msg, byte[] signature) { var verifier = new Ed25519Signer(); verifier.Init(false, publicKey); verifier.BlockUpdate(msg, 0, msg.Length); return(verifier.VerifySignature(signature)); }
public static bool Verify(byte[] data, byte[] signature, string publicKey) { Ed25519Signer verifier = new Ed25519Signer(); verifier.Init(false, publicKey.ToPublicKeyCipherParamsFromHex()); verifier.BlockUpdate(data, 0, data.Length); return(verifier.VerifySignature(signature)); }
public static byte[] Sign(byte[] data, string privateKey) { Ed25519Signer signer = new Ed25519Signer(); signer.Init(true, privateKey.ToPrivateKeyCipherParamsFromHex()); signer.BlockUpdate(data, 0, data.Length); return(signer.GenerateSignature()); }
bool ICryptoLibrary.VerifyEd25519(byte[] plainText, byte[] signature, byte[] publicKey) { var signer = new Ed25519Signer(); signer.Init(false, new Ed25519PublicKeyParameters(publicKey, 0)); signer.BlockUpdate(plainText, 0, plainText.Length); return(signer.VerifySignature(signature)); }
public byte[] sign(byte[] msg) { var signer = new Ed25519Signer(); signer.Init(true, privateKey); signer.BlockUpdate(msg, 0, msg.Length); return(signer.GenerateSignature()); }
/// <summary> /// Sign the given bytes, and wrap in Signature. /// </summary> /// <param name="bytes">bytes the data to sign</param> /// <returns>a signature</returns> private Signature RawSignBytes(byte[] bytes) { var signer = new Ed25519Signer(); signer.Init(true, privateKeyPair.Private); signer.BlockUpdate(bytes, 0, bytes.Length); byte[] signature = signer.GenerateSignature(); return(new Signature(signature)); }
// Sings data with signer’s private key using Bouncy Castle public static string Sign(string data, string privateKey) { var signer = new Ed25519Signer(); signer.Init(true, new Ed25519PrivateKeyParameters(Hex.Decode(privateKey), 0)); signer.BlockUpdate(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetBytes(data).Length); return(Hex.ToHexString(signer.GenerateSignature())); }
public override bool Verify(byte[] input, byte[] signature) { var validator = new Ed25519Signer(); validator.Init(false, edDsaKey.KeyParameters); validator.BlockUpdate(input, 0, input.Length); return(validator.VerifySignature(signature)); }
public override byte[] Sign(byte[] input) { var signer = new Ed25519Signer(); signer.Init(true, edDsaKey.KeyParameters); signer.BlockUpdate(input, 0, input.Length); return(signer.GenerateSignature()); }
// Verifies signature with signer’s public key using Bouncy Castle public static bool Verify(string data, string signature, string publicKey) { var verifier = new Ed25519Signer(); verifier.Init(false, new Ed25519PublicKeyParameters(Hex.Decode(publicKey), 0)); verifier.BlockUpdate(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetBytes(data).Length); return(verifier.VerifySignature(Hex.Decode(signature))); }
private void BasicSigTest() { Ed25519PrivateKeyParameters privateKey = new Ed25519PrivateKeyParameters( Hex.DecodeStrict("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"), 0); Ed25519PublicKeyParameters publicKey = new Ed25519PublicKeyParameters( Hex.DecodeStrict("d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"), 0); byte[] sig = Hex.Decode("e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"); ISigner signer = new Ed25519Signer(); signer.Init(true, privateKey); IsTrue(AreEqual(sig, signer.GenerateSignature())); signer.Init(false, publicKey); IsTrue(signer.VerifySignature(sig)); }
/// <summary> /// Perform signature verification against the sender address /// </summary> /// <param name="address">Address to verify</param> /// <returns>bool</returns> public bool Verify(Address address) { if (this.logic == null) { return(false); } else if (this.sig != null && this.msig != null) { return(false); } else { try { Logic.CheckProgram(this.logic, this.args); } catch (Exception) { return(false); } if (this.sig == null && this.msig == null) { try { return(address.Equals(this.ToAddress())); } catch (Exception) { return(false); } } if (this.sig != null) { try { var pk = new Ed25519PublicKeyParameters(address.Bytes, 0); var signer = new Ed25519Signer(); signer.Init(false, pk); //false代表用于VerifySignature signer.BlockUpdate(this.BytesToSign(), 0, this.BytesToSign().Length); return(signer.VerifySignature(this.sig.Bytes)); } catch (Exception err) { Console.WriteLine("Message = " + err.Message); return(false); } } else { return(this.msig.Verify(this.BytesToSign())); } } }
public bool Verify(byte[] msg, byte[] sig, byte[] pubKey) { var keyedHash = Blake2b.GetDigest(msg); var publicKey = new Ed25519PublicKeyParameters(pubKey, 0); var verifier = new Ed25519Signer(); verifier.Init(false, publicKey); verifier.BlockUpdate(keyedHash, 0, keyedHash.Length); return(verifier.VerifySignature(sig)); }
public Signature Sign(byte[] msg, byte[] prvKey) { var digest = Blake2b.GetDigest(msg); var privateKey = new Ed25519PrivateKeyParameters(prvKey, 0); var signer = new Ed25519Signer(); signer.Init(true, privateKey); signer.BlockUpdate(digest, 0, digest.Length); return(new Signature(signer.GenerateSignature(), _SignaturePrefix)); }
public bool VerifyStamp(TimestampDao timestamp) { var fileHash = timestamp.FileHash.ToUpper().FromBase32(); var signature = timestamp.Signature.ToUpper().FromBase32(); var publicKey = new Ed25519PublicKeyParameters(timestamp.PublicKey.ToUpper().FromBase32(), 0); var verifier = new Ed25519Signer(); verifier.Init(false, publicKey); verifier.BlockUpdate(fileHash, 0, fileHash.Length); return(verifier.VerifySignature(signature)); }
/// <summary> /// Implement the signing algorithm. In the case of an Ed25519 /// it will use the private key to sign the transaction and /// return immediately. In the case of the callback method, it /// will pass the invoice to the async method and async await /// for the method to return. /// </summary> /// <param name="invoice"> /// The information for the transaction, including the Transaction /// ID, Memo and serialized bytes of the crypto transfers and other /// embedded information making up the transaction. /// </param> /// <returns></returns> async Task ISignatory.SignAsync(IInvoice invoice) { switch (_type) { case Type.Ed25519: var privateKey = (Ed25519PrivateKeyParameters)_data; var ed25519Signer = new Ed25519Signer(); ed25519Signer.Init(true, privateKey); ed25519Signer.BlockUpdate(invoice.TxBytes.ToArray(), 0, invoice.TxBytes.Length); invoice.AddSignature( KeyType.Ed25519, privateKey.GeneratePublicKey().GetEncoded()[..6],
/// <summary> /// Performs signature verification /// </summary> /// <param name="message">raw message to verify</param> /// <returns>bool</returns> public bool Verify(byte[] message) { if (this.version == 1 && this.threshold > 0 && this.subsigs.Count != 0) { if (this.threshold > this.subsigs.Count) { return(false); } else { int verifiedCount = 0; Signature emptySig = new Signature(); for (int i = 0; i < this.subsigs.Count; ++i) { MultisigSubsig subsig = subsigs[i]; if (!subsig.sig.Equals(emptySig)) { try { var pk = subsig.key; var signer = new Ed25519Signer(); signer.Init(false, pk); //for verify signer.BlockUpdate(message, 0, message.Length); bool verified = signer.VerifySignature(subsig.sig.Bytes); if (verified) { ++verifiedCount; } } catch (Exception var9) { throw new ArgumentException("verification of subsig " + i + "failed", var9); } } } if (verifiedCount < this.threshold) { return(false); } else { return(true); } } } else { return(false); } }
public override string Sign(string payload, string footer, SigningCredentials signingCredentials) { if (payload == null) { throw new ArgumentNullException(nameof(payload)); } if (signingCredentials == null) { throw new ArgumentNullException(nameof(signingCredentials)); } if (signingCredentials.Key.GetType() != typeof(EdDsaSecurityKey)) { throw new SecurityTokenInvalidSigningKeyException($"PASETO v2 requires a key of type {typeof(EdDsaSecurityKey)}"); } if (signingCredentials.Algorithm != ExtendedSecurityAlgorithms.EdDsa) { throw new SecurityTokenInvalidSigningKeyException($"PASETO v2 requires a key for configured for the '{ExtendedSecurityAlgorithms.EdDsa}' algorithm"); } var privateKey = (EdDsaSecurityKey)signingCredentials.Key; if (privateKey.PrivateKeyStatus != PrivateKeyStatus.Exists) { throw new SecurityTokenInvalidSigningKeyException($"Missing private key"); } var payloadBytes = Encoding.UTF8.GetBytes(payload); var messageToSign = PreAuthEncode(new[] { Encoding.UTF8.GetBytes(PublicHeader), payloadBytes, Encoding.UTF8.GetBytes(footer ?? string.Empty) }); var signer = new Ed25519Signer(); signer.Init(true, privateKey.KeyParameters); signer.BlockUpdate(messageToSign, 0, messageToSign.Length); var signature = signer.GenerateSignature(); var token = $"{PublicHeader}{Base64UrlEncoder.Encode(payloadBytes.Combine(signature))}"; if (!string.IsNullOrWhiteSpace(footer)) { token += $".{Base64UrlEncoder.Encode(footer)}"; } return(token); }
internal static void Sign(IInvoice invoice, Ed25519PrivateKeyParameters privateKey) { var ed25519Signer = new Ed25519Signer(); ed25519Signer.Init(true, privateKey); ed25519Signer.BlockUpdate(invoice.TxBytes.ToArray(), 0, invoice.TxBytes.Length); var signature = ed25519Signer.GenerateSignature(); ed25519Signer.Reset(); var publicKey = privateKey.GeneratePublicKey().GetEncoded(); var prefix = new ReadOnlyMemory <byte>(publicKey, 0, Math.Min(Math.Max(6, invoice.MinimumDesiredPrefixSize), publicKey.Length)); invoice.AddSignature(KeyType.Ed25519, prefix, signature); }
/// <summary> /// verifyBytes verifies that the signature for the message is valid for the public key. /// The message should have been prepended with "MX" when signing. /// </summary> /// <param name="message">the message that was signed</param> /// <param name="signature">signature</param> /// <returns>true if the signature is valid</returns> public bool VerifyBytes(byte[] message, Signature signature) { var pk = new Ed25519PublicKeyParameters(this.Bytes, 0); // prepend the message prefix List <byte> prefixBytes = new List <byte>(BYTES_SIGN_PREFIX); prefixBytes.AddRange(message); // verify signature // Generate new signature var signer = new Ed25519Signer(); signer.Init(false, pk); signer.BlockUpdate(prefixBytes.ToArray(), 0, prefixBytes.ToArray().Length); return(signer.VerifySignature(signature.Bytes)); }
byte[] ICryptoLibrary.SignEd25519(byte[] plainText, byte[] privateKey) { if (privateKey == null) { throw new ArgumentException(nameof(privateKey)); } if (plainText == null) { throw new ArgumentException(nameof(plainText)); } var signer = new Ed25519Signer(); signer.Init(true, new Ed25519PrivateKeyParameters(privateKey, 0)); signer.BlockUpdate(plainText, 0, plainText.Length); return(signer.GenerateSignature()); }
/// <inheritdoc/> public override byte[] Sign(byte[] input) { if (input == null || input.Length == 0) { throw new ArgumentNullException(nameof(input)); } if (EdDsaKey.Curve.Equals("Ed25519")) { var privateKey = (Ed25519PrivateKeyParameters)EdDsaKey.KeyParameter; var signer = new Ed25519Signer(); signer.Init(forSigning: true, privateKey); signer.BlockUpdate(input, off: 0, len: input.Length); return(signer.GenerateSignature()); } throw new ArgumentException($"Unsupported EdDSA curve: \"{EdDsaKey.Curve}\"."); }
private static void EdDsaWithBouncyCastle() { // message to sign & verify var message = Encoding.UTF8.GetBytes("Bob Loblaw"); // private/public key generation var keyPairGenerator = new Ed25519KeyPairGenerator(); keyPairGenerator.Init(new Ed25519KeyGenerationParameters(new SecureRandom())); var keyPair = keyPairGenerator.GenerateKeyPair(); var privateKey = (Ed25519PrivateKeyParameters)keyPair.Private; var publicKey = (Ed25519PublicKeyParameters)keyPair.Public; // keys are 32-bytes each var privateKeyBytes = privateKey.GetEncoded(); var publicKeyBytes = publicKey.GetEncoded(); Console.WriteLine("Private key = " + Convert.ToBase64String(privateKeyBytes)); Console.WriteLine("Public key = " + Convert.ToBase64String(publicKeyBytes)); // signature generation var signer = new Ed25519Signer(); signer.Init(true, privateKey); signer.BlockUpdate(message, 0, message.Length); byte[] signature = signer.GenerateSignature(); Console.WriteLine("Signature = " + Convert.ToBase64String(signature)); // signature validation var validator = new Ed25519Signer(); validator.Init(false, publicKey); validator.BlockUpdate(message, 0, message.Length); bool isValidSignature = validator.VerifySignature(signature); Console.WriteLine("Signature is valid: " + isValidSignature); }
/// <summary> /// Verifies the signature. /// </summary> /// <param name="file">The file.</param> /// <param name="signature">The signature.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool VerifySignature(FileInfo file, string signature) { if (!KeysExist()) { Console.WriteLine("Keys do not exist"); return(false); } if (signature == null) { Console.WriteLine("Signature at path {0} is null", file.FullName); return(false); } var data = File.ReadAllBytes(file.FullName); var validator = new Ed25519Signer(); validator.Init(false, new Ed25519PublicKeyParameters(GetPublicKey(), 0)); validator.BlockUpdate(data, 0, data.Length); return(validator.VerifySignature(Convert.FromBase64String(signature))); }
/// <summary> /// Gets the signature for file. /// </summary> /// <param name="file">The file.</param> /// <returns>System.String.</returns> public string GetSignatureForFile(FileInfo file) { if (!KeysExist()) { Console.WriteLine("Keys do not exist"); return(null); } if (!file.Exists) { Console.Error.WriteLine("Target binary " + file.FullName + " does not exists"); return(null); } var data = File.ReadAllBytes(file.FullName); var signer = new Ed25519Signer(); signer.Init(true, new Ed25519PrivateKeyParameters(GetPrivateKey(), 0)); signer.BlockUpdate(data, 0, data.Length); return(Convert.ToBase64String(signer.GenerateSignature())); }
/// <inheritdoc/> public override bool Verify(byte[] input, byte[] signature) { if (input == null || input.Length == 0) { throw new ArgumentNullException(nameof(input)); } if (signature == null || signature.Length == 0) { throw new ArgumentNullException(nameof(signature)); } if (EdDsaKey.Curve.Equals("Ed25519")) { var publicKey = (Ed25519PublicKeyParameters)EdDsaKey.KeyParameter; var validator = new Ed25519Signer(); validator.Init(forSigning: false, publicKey); validator.BlockUpdate(input, off: 0, len: input.Length); return(validator.VerifySignature(signature)); } throw new ArgumentException($"Unsupported EdDSA curve: \"{EdDsaKey.Curve}\"."); }
/// <summary> /// Create a Ed25519Checker object from the given parameters /// </summary> /// <param name="mode">The security mode of the validator. Controls what needs to be set in order to validate /// an app cast and its items.</param> /// <param name="publicKey">the base 64 public key as a string</param> /// <param name="publicKeyFile">the public key file</param> public Ed25519Checker(SecurityMode mode, string publicKey = null, string publicKeyFile = "NetSparkle_Ed25519.pub") { SecurityMode = mode; if (string.IsNullOrEmpty(publicKey)) { Stream data = TryGetResourceStream(publicKeyFile); if (data == null) { data = TryGetFileResource(publicKeyFile, data); } if (data != null) { using (StreamReader reader = new StreamReader(data)) { publicKey = reader.ReadToEnd(); } } } if (!string.IsNullOrEmpty(publicKey)) { try { _signer = new Ed25519Signer(); byte[] pubKeyBytes = Convert.FromBase64String(publicKey); var cipherParams = new Ed25519PublicKeyParameters(pubKeyBytes, 0); _signer.Init(false, cipherParams); } catch { _signer = null; } } }