public void TestStdSignVerify()
        {
            IPrivateKey privateKey = _wrapper.GeneratePrivateKey();

            byte[]     message    = Encoding.UTF8.GetBytes("fa la la la");
            byte[]     context    = Encoding.UTF8.GetBytes("context");
            ISignature signature  = _wrapper.Sign(privateKey, message, context);
            bool       isVerified = _wrapper.Verify(signature, message, context);

            isVerified.Should().BeTrue();
        }
Exemple #2
0
        public void TestFailureSigningVerification()
        {
            var key1 = _context.GeneratePrivateKey();

            var data           = Encoding.UTF8.GetBytes("Testing testing 1 2 3");
            var signingContext = Encoding.UTF8.GetBytes("Testing testing 1 2 3 context");
            var signature      = _context.Sign(key1, data, signingContext);

            var key2       = _context.GeneratePrivateKey();
            var publicKey2 = _context.GetPublicKeyFromPrivateKey(key2);

            var invalidSignature = _context.GetSignatureFromBytes(signature.SignatureBytes, publicKey2.Bytes);

            _context.Verify(invalidSignature, data, signingContext)
            .Should().BeFalse("signature should not verify with incorrect key");
        }
Exemple #3
0
        public static TransactionBroadcast Sign(this TransactionBroadcast transaction,
                                                ICryptoContext cryptoContext,
                                                IPrivateKey privateKey,
                                                SigningContext context)
        {
            var clone = transaction.Clone();

            if (transaction.Signature?.RawBytes.Length == cryptoContext.SignatureLength)
            {
                Logger.Debug("The transaction was already signed, returning a clone.");
                return(clone);
            }

            clone.Signature = null;
            var signatureBytes = cryptoContext.Sign(privateKey, clone.ToByteArray(),
                                                    context.ToByteArray()).SignatureBytes;

            clone.Signature = new Signature
            {
                RawBytes       = signatureBytes.ToByteString(),
                SigningContext = context
            };

            return(clone);
        }
Exemple #4
0
        public ISignature Sign(ReadOnlySpan <byte> data, SigningContext signingContext)
        {
            var privateKey = GetPrivateKey(KeyRegistryTypes.DefaultKey);

            using var pooled = signingContext.SerializeToPooledBytes();

            return(_cryptoContext.Sign(privateKey, data, pooled.Span));
        }
Exemple #5
0
        private static Signature GeneratePublicEntrySignature(PublicEntry publicEntry,
                                                              ICryptoContext cryptoContext,
                                                              IPrivateKey privateKey,
                                                              SigningContext context)
        {
            publicEntry.Signature = null;
            var signatureBytes = cryptoContext.Sign(privateKey, publicEntry.ToByteArray(),
                                                    context.ToByteArray()).SignatureBytes;

            return(new Signature
            {
                RawBytes = signatureBytes.ToByteString(),
                SigningContext = context
            });
        }
Exemple #6
0
        /// <summary>
        ///     Signs message using provided private key and returns the signature.
        /// </summary>
        /// <param name="crypto"></param>
        /// <param name="privateKey"></param>
        /// <param name="message"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static ISignature Sign(this ICryptoContext crypto, IPrivateKey privateKey, IMessage message, IMessage context)
        {
            ProtoPreconditions.CheckNotNull(message, nameof(message));
            ProtoPreconditions.CheckNotNull(context, nameof(context));

            var messageSize = message.CalculateSize();
            var contextSize = context.CalculateSize();
            var array       = ArrayPool <byte> .Shared.Rent(messageSize + contextSize);

            using (var output = new CodedOutputStream(array))
            {
                message.WriteTo(output);
                context.WriteTo(output);
            }

            var signature = crypto.Sign(privateKey, array.AsSpan(0, messageSize), array.AsSpan(messageSize, contextSize));

            ArrayPool <byte> .Shared.Return(array);

            return(signature);
        }
 private ISignature Sign(byte[] data, SigningContext signingContext, IPrivateKey privateKey)
 {
     return(_cryptoContext.Sign(privateKey, data, signingContext.ToByteArray()));
 }