Пример #1
0
        /// <summary>
        /// Encodes this instance.
        /// </summary>
        public JwsPayload Sign(object payload, string nonce = null)
        {
            var jsonSettings = JsonUtil.CreateSettings();

            var protectedHeader = new
            {
                nonce = nonce,
                alg   = keyPair.Algorithm.ToJwsAlgorithm(),
                jwk   = keyPair.JsonWebKey,
            };

            var entityJson          = JsonConvert.SerializeObject(payload, Formatting.None, jsonSettings);
            var protectedHeaderJson = JsonConvert.SerializeObject(protectedHeader, Formatting.None, jsonSettings);

            var payloadEncoded         = JwsConvert.ToBase64String(Encoding.UTF8.GetBytes(entityJson));
            var protectedHeaderEncoded = JwsConvert.ToBase64String(Encoding.UTF8.GetBytes(protectedHeaderJson));

            var signature              = $"{protectedHeaderEncoded}.{payloadEncoded}";
            var signatureBytes         = Encoding.ASCII.GetBytes(signature);
            var signedSignatureBytes   = keyPair.SignData(signatureBytes);
            var signedSignatureEncoded = JwsConvert.ToBase64String(signedSignatureBytes);

            var body = new JwsPayload
            {
                Protected = protectedHeaderEncoded,
                Payload   = payloadEncoded,
                Signature = signedSignatureEncoded
            };

            return(body);
        }
Пример #2
0
        public void CanConvertToBase64String()
        {
            foreach (var s in new[]
            {
                "a", "ab", "abc", "abcd"
            })
            {
                var data     = Encoding.UTF8.GetBytes(s);
                var str      = JwsConvert.ToBase64String(data);
                var reverted = JwsConvert.FromBase64String(str);
                Assert.Equal(data, reverted);
            }

            Assert.Throws <AcmeException>(() => JwsConvert.FromBase64String("/not a valid base 64 string/!"));
        }
Пример #3
0
        public void CanConvertToBase64String()
        {
            var data = Enumerable
                       .Range(0, 1000)
                       .Select(i => (byte)i)
                       .ToArray();

            var str = JwsConvert.ToBase64String(data);

            var reverted = JwsConvert.FromBase64String(str);

            Assert.Equal(data, reverted);

            Assert.Throws <Exception>(() => JwsConvert.FromBase64String("/not a valid base 64 string/!"));
        }