Exemple #1
0
        // Private Methods

        private JwsData Sign(object header, object body)
        {
            var jsonSettings = JsonSettings.CreateSettings();
            var entityJson   = body == null ?
                               "" :
                               JsonConvert.SerializeObject(body, Formatting.None, jsonSettings);
            var protectedHeaderJson = JsonConvert.SerializeObject(header, Formatting.None, jsonSettings);

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

            var signature              = $"{protectedHeaderEncoded}.{entityEncoded}";
            var signatureBytes         = Encoding.UTF8.GetBytes(signature);
            var signedSignatureBytes   = _key.SignData(signatureBytes);
            var signedSignatureEncoded = JwsConvert.ToBase64String(signedSignatureBytes);

            return(new JwsData
            {
                Protected = protectedHeaderEncoded,
                Payload = entityEncoded,
                Signature = signedSignatureEncoded
            });
        }