Example #1
0
        internal byte[] Sign(byte[] message, string secretPhrase)
        {
            var p = new byte[32];
            var s = new byte[32];

            using (var incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256))
                using (var sha256 = SHA256.Create())
                {
                    Curve25519.Keygen(p, s, sha256.ComputeHash(Encoding.UTF8.GetBytes(secretPhrase)));

                    var m = sha256.ComputeHash(message);
                    incrementalHash.AppendData(m);
                    incrementalHash.AppendData(s);
                    var x = incrementalHash.GetHashAndReset();

                    var y = new byte[32];
                    Curve25519.Keygen(y, null, x);

                    incrementalHash.AppendData(m);
                    incrementalHash.AppendData(y);
                    var h = incrementalHash.GetHashAndReset();

                    var v = new byte[32];
                    Curve25519.Sign(v, h, x, s);

                    var signature = v.Concat(h).ToArray();
                    return(signature);
                }
        }
Example #2
0
        public byte[] Sign(byte[] message, string secretPhrase)
        {
            var p = new byte[32];
            var s = new byte[32];

            var sha256 = SHA256.Create();

            Curve25519.Keygen(p, s, sha256.ComputeHash(Encoding.UTF8.GetBytes(secretPhrase)));

            var m = sha256.ComputeHash(message);

            sha256.TransformBlock(m, 0, m.Length, m, 0);
            sha256.TransformFinalBlock(s, 0, s.Length);
            var x = sha256.Hash;

            var y = new byte[32];

            Curve25519.Keygen(y, null, x);

            sha256 = SHA256.Create();
            sha256.TransformBlock(m, 0, m.Length, m, 0);
            sha256.TransformFinalBlock(y, 0, y.Length);
            var h = sha256.Hash;

            var v = new byte[32];

            Curve25519.Sign(v, h, x, s);

            var signature = v.Concat(h).ToArray();

            return(signature);
        }
Example #3
0
 private byte[] GetPrivateKeyBytes(string secretPhrase)
 {
     using (var sha256 = SHA256.Create())
     {
         var privateKeyBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(secretPhrase));
         Curve25519.Clamp(privateKeyBytes);
         return(privateKeyBytes);
     }
 }
Example #4
0
        private byte[] GetSharedSecret(byte[] theirPublicKey, byte[] nonce, byte[] myPrivateKey)
        {
            var sharedSecret = new byte[32];

            Curve25519.Curve(sharedSecret, myPrivateKey, theirPublicKey);
            for (var i = 0; i < 32; i++)
            {
                sharedSecret[i] ^= nonce[i];
            }
            return(ComputeHash(sharedSecret));
        }
Example #5
0
        internal BinaryHexString GetPublicKey(string secretPhrase)
        {
            var publicKey           = new byte[32];
            var encodedSecretPhrase = Encoding.UTF8.GetBytes(secretPhrase);
            var hashedSecretPhrase  = ComputeHash(encodedSecretPhrase);

            Curve25519.Keygen(publicKey, null, hashedSecretPhrase);
            var binaryHexString = new BinaryHexString(publicKey);

            return(binaryHexString);
        }
Example #6
0
        private bool Verify(byte[] signature, byte[] message, byte[] publicKey, bool enforceCanonical)
        {
            try
            {
                if (signature.Length != 64 ||
                    (enforceCanonical && !Curve25519.IsCanonicalSignature(signature)) ||
                    (enforceCanonical && !Curve25519.IsCanonicalPublicKey(publicKey)))
                {
                    return(false);
                }

                var y = new byte[32];
                var v = signature.Take(32).ToArray();
                var h = signature.Skip(32).ToArray();

                Curve25519.Verify(y, v, h, publicKey);
                var h2 = HashIncremental(message, y);
                return(h.SequenceEqual(h2));
            }
            catch (Exception)
            {
                return(false);
            }
        }