예제 #1
0
        /// <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)
        {
            var signer = new Ed448Signer(ByteConvert.StringToAsciiBytes("context"));
            Ed448PrivateKeyParameters privKey = null;

            try
            {
                privKey = (Ed448PrivateKeyParameters)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 Ed448 key parameter\n" +
                                 "Verify that the key is not corrupted.\n" +
                                 "- or - Verify that the correct key is selected.";
                throw new CryptoException(message, exception);
            }
            signer.Init(true, privKey);
            signer.BlockUpdate(data, 0, data.Length);
            var signature = signer.GenerateSignature();

            return(signature);
        }
예제 #2
0
        /// <summary>
        /// Verifies a signature to be authentic
        /// </summary>
        /// <param name="originalSignature">The signature which is be verified</param>
        /// <param name="publicKey">the public key used for the verification</param>
        /// <param name="data">the data which is signed</param>
        /// <returns>true if signature is authentic, false if not</returns>
        public bool Verify(byte[] originalSignature, byte[] publicKey, byte[] data)
        {
            Ed448PublicKeyParameters pubKey = null;

            try
            {
                pubKey = (Ed448PublicKeyParameters)CreateAsymmetricKeyParameterFromPublicKeyInfo(publicKey);
            }
            catch (InvalidCastException exception)
            {
                string message = "Public Key Import Failed!\n" +
                                 $"{exception.Message}.\n" +
                                 "The contents of the source do not represent a valid Ed448 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 Ed448Signer(ByteConvert.StringToAsciiBytes("context"));

            signer.Init(false, pubKey);
            signer.BlockUpdate(data, 0, data.Length);
            return(signer.VerifySignature(originalSignature));
        }