/// <summary>
        /// Generates a Jws Signature.
        /// </summary>
        /// <param name="header"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        public string GenerateSignature(Dictionary <string, object> header, Dictionary <string, object> payload)
        {
            string securedInput = SecureInput(header, payload);

            byte[] message = Encoding.UTF8.GetBytes(securedInput);


            //SHA256Cng sha256Hasher = new SHA256Cng();
            SHA256 sha256Hasher = SHA256.Create();

            byte[] hashedMessage = sha256Hasher.ComputeHash(message);

            ECDsaSigner signer = new ECDsaSigner();

            signer.Init(true, _privateKey);
            BigInteger[] results = signer.GenerateSignature(hashedMessage);

            // Concated to create signature
            var a = results[0].ToByteArrayUnsigned();
            var b = results[1].ToByteArrayUnsigned();

            // a,b are required to be exactly the same length of bytes
            if (a.Length != b.Length)
            {
                int largestLength = Math.Max(a.Length, b.Length);
                a = ByteArrayPadLeft(a, largestLength);
                b = ByteArrayPadLeft(b, largestLength);
            }

            string signature = UrlBase64.Encode(a.Concat(b).ToArray());

            return(String.Format("{0}.{1}", securedInput, signature));
        }
        private static string SecureInput(Dictionary <string, object> header, Dictionary <string, object> payload)
        {
            string encodeHeader  = UrlBase64.Encode(Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(header)));
            string encodePayload = UrlBase64.Encode(Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(payload)));

            return(String.Format("{0}.{1}", encodeHeader, encodePayload));
        }
Beispiel #3
0
        public static EncryptionResult Encrypt(string userKey, string userSecret, string payload)
        {
            byte[] userKeyBytes    = UrlBase64.Decode(userKey);
            byte[] userSecretBytes = UrlBase64.Decode(userSecret);
            byte[] payloadBytes    = Encoding.UTF8.GetBytes(payload);

            return(Encrypt(userKeyBytes, userSecretBytes, payloadBytes));
        }