Esempio n. 1
0
        private static string SecureInput(Dictionary <string, object> header, Dictionary <string, object> payload)
        {
            var encodeHeader  = UrlBase64.Encode(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header)));
            var encodePayload = UrlBase64.Encode(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)));

            return($"{encodeHeader}.{encodePayload}");
        }
Esempio n. 2
0
        /// <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)
        {
            var securedInput = SecureInput(header, payload);
            var message      = Encoding.UTF8.GetBytes(securedInput);

            var hashedMessage = Sha256Hash(message);

            var signer = new ECDsaSigner();

            signer.Init(true, _privateKey);
            var 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)
            {
                var largestLength = Math.Max(a.Length, b.Length);
                a = ByteArrayPadLeft(a, largestLength);
                b = ByteArrayPadLeft(b, largestLength);
            }

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

            return($"{securedInput}.{signature}");
        }