< Summary

Class:Azure.Storage.Cryptography.Models.EncryptionDataSerializer
Assembly:Azure.Storage.Queues
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\ClientsideEncryption\Models\EncryptionDataSerializer.cs
Covered lines:58
Uncovered lines:15
Coverable lines:73
Total lines:198
Line coverage:79.4% (58 of 73)
Covered branches:30
Total branches:30
Branch coverage:100% (30 of 30)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Serialize(...)-0%100%
SerializeEncryptionData(...)-0%100%
WriteEncryptionData(...)-100%100%
WriteWrappedKey(...)-100%100%
WriteEncryptionAgent(...)-100%100%
WriteDictionary(...)-100%100%
Deserialize(...)-0%100%
DeserializeEncryptionData(...)-0%100%
ReadEncryptionData(...)-100%100%
ReadPropertyValue(...)-100%100%
ReadPropertyValue(...)-100%100%
ReadPropertyValue(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Common\src\Shared\ClientsideEncryption\Models\EncryptionDataSerializer.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Text;
 7using System.Text.Json;
 8
 9namespace Azure.Storage.Cryptography.Models
 10{
 11    internal static class EncryptionDataSerializer
 12    {
 13        private const string EncryptionAgent_EncryptionVersionName = "Protocol";
 14
 15        #region Serialize
 16
 17        /// <summary>
 18        /// Serializes an EncryptionData instance into JSON.
 19        /// </summary>
 20        /// <param name="data">Data to serialize.</param>
 21        /// <returns>The JSON string.</returns>
 22        public static string Serialize(EncryptionData data)
 23        {
 024            return Encoding.UTF8.GetString(SerializeEncryptionData(data).ToArray());
 25        }
 26
 27        /// <summary>
 28        /// Serializes an EncryptionData instance into JSON.
 29        /// </summary>
 30        /// <param name="data">Data to serialize.</param>
 31        /// <returns>The JSON UTF8 bytes.</returns>
 32        private static ReadOnlyMemory<byte> SerializeEncryptionData(EncryptionData data)
 33        {
 034            var writer = new Core.ArrayBufferWriter<byte>();
 035            using var json = new Utf8JsonWriter(writer);
 36
 037            json.WriteStartObject();
 038            WriteEncryptionData(json, data);
 039            json.WriteEndObject();
 40
 041            json.Flush();
 042            return writer.WrittenMemory;
 043        }
 44
 45        /// <summary>
 46        /// Serializes an EncryptionData instance into JSON and writes it to the given JSON writer.
 47        /// </summary>
 48        /// <param name="json">The writer to write the serialization to.</param>
 49        /// <param name="data">Data to serialize.</param>
 50        public static void WriteEncryptionData(Utf8JsonWriter json, EncryptionData data)
 51        {
 652            json.WriteString(nameof(data.EncryptionMode), data.EncryptionMode);
 53
 654            json.WriteStartObject(nameof(data.WrappedContentKey));
 655            WriteWrappedKey(json, data.WrappedContentKey);
 656            json.WriteEndObject();
 57
 658            json.WriteStartObject(nameof(data.EncryptionAgent));
 659            WriteEncryptionAgent(json, data.EncryptionAgent);
 660            json.WriteEndObject();
 61
 662            json.WriteString(nameof(data.ContentEncryptionIV), Convert.ToBase64String(data.ContentEncryptionIV));
 63
 664            json.WriteStartObject(nameof(data.KeyWrappingMetadata));
 665            WriteDictionary(json, data.KeyWrappingMetadata);
 666            json.WriteEndObject();
 667        }
 68
 69        private static void WriteWrappedKey(Utf8JsonWriter json, KeyEnvelope key)
 70        {
 671            json.WriteString(nameof(key.KeyId), key.KeyId);
 672            json.WriteString(nameof(key.EncryptedKey), Convert.ToBase64String(key.EncryptedKey));
 673            json.WriteString(nameof(key.Algorithm), key.Algorithm);
 674        }
 75
 76        private static void WriteEncryptionAgent(Utf8JsonWriter json, EncryptionAgent encryptionAgent)
 77        {
 678            json.WriteString(EncryptionAgent_EncryptionVersionName, encryptionAgent.EncryptionVersion.Serialize());
 679            json.WriteString(nameof(encryptionAgent.EncryptionAlgorithm), encryptionAgent.EncryptionAlgorithm.ToString()
 680        }
 81
 82        private static void WriteDictionary(Utf8JsonWriter json, IDictionary<string, string> dictionary)
 83        {
 2484            foreach (var entry in dictionary)
 85            {
 686                json.WriteString(entry.Key, entry.Value);
 87            }
 688        }
 89        #endregion
 90
 91        #region Deserialize
 92        /// <summary>
 93        /// Deserializes an EncryptionData instance from JSON.
 94        /// </summary>
 95        /// <param name="serializedData">The serialized data string.</param>
 96        /// <returns>The instance.</returns>
 97        public static EncryptionData Deserialize(string serializedData)
 98        {
 099            var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(serializedData));
 0100            return DeserializeEncryptionData(ref reader);
 101        }
 102
 103        /// <summary>
 104        /// Reads an EncryptionData instance from a JSON reader.
 105        /// </summary>
 106        /// <param name="reader">The reader to parse an EncryptionData isntance from.</param>
 107        /// <returns>The instance.</returns>
 108        public static EncryptionData DeserializeEncryptionData(ref Utf8JsonReader reader)
 109        {
 0110            using JsonDocument json = JsonDocument.ParseValue(ref reader);
 0111            JsonElement root = json.RootElement;
 0112            return ReadEncryptionData(root);
 0113        }
 114
 115        /// <summary>
 116        /// Reads an EncryptionData instance from a parsed JSON object.
 117        /// </summary>
 118        /// <param name="root">The JSON object model.</param>
 119        /// <returns>The instance.</returns>
 120        public static EncryptionData ReadEncryptionData(JsonElement root)
 121        {
 6122            var data = new EncryptionData();
 52123            foreach (var property in root.EnumerateObject())
 124            {
 20125                ReadPropertyValue(data, property);
 126            }
 6127            return data;
 128        }
 129
 130        private static void ReadPropertyValue(EncryptionData data, JsonProperty property)
 131        {
 20132            if (property.NameEquals(nameof(data.EncryptionMode)))
 133            {
 4134                data.EncryptionMode = property.Value.GetString();
 135            }
 16136            else if (property.NameEquals(nameof(data.WrappedContentKey)))
 137            {
 4138                var key = new KeyEnvelope();
 32139                foreach (var subProperty in property.Value.EnumerateObject())
 140                {
 12141                    ReadPropertyValue(key, subProperty);
 142                }
 4143                data.WrappedContentKey = key;
 144            }
 12145            else if (property.NameEquals(nameof(data.EncryptionAgent)))
 146            {
 4147                var agent = new EncryptionAgent();
 24148                foreach (var subProperty in property.Value.EnumerateObject())
 149                {
 8150                    ReadPropertyValue(agent, subProperty);
 151                }
 4152                data.EncryptionAgent = agent;
 153            }
 8154            else if (property.NameEquals(nameof(data.ContentEncryptionIV)))
 155            {
 4156                data.ContentEncryptionIV = Convert.FromBase64String(property.Value.GetString());
 157            }
 4158            else if (property.NameEquals(nameof(data.KeyWrappingMetadata)))
 159            {
 4160                var metadata = new Dictionary<string, string>();
 16161                foreach (var entry in property.Value.EnumerateObject())
 162                {
 4163                    metadata.Add(entry.Name, entry.Value.GetString());
 164                }
 4165                data.KeyWrappingMetadata = metadata;
 166            }
 4167        }
 168
 169        private static void ReadPropertyValue(KeyEnvelope key, JsonProperty property)
 170        {
 12171            if (property.NameEquals(nameof(key.Algorithm)))
 172            {
 4173                key.Algorithm = property.Value.GetString();
 174            }
 8175            else if (property.NameEquals(nameof(key.EncryptedKey)))
 176            {
 4177                key.EncryptedKey = Convert.FromBase64String(property.Value.GetString());
 178            }
 4179            else if (property.NameEquals(nameof(key.KeyId)))
 180            {
 4181                key.KeyId = property.Value.GetString();
 182            }
 4183        }
 184
 185        private static void ReadPropertyValue(EncryptionAgent agent, JsonProperty property)
 186        {
 8187            if (property.NameEquals(nameof(agent.EncryptionAlgorithm)))
 188            {
 4189                agent.EncryptionAlgorithm = new ClientSideEncryptionAlgorithm(property.Value.GetString());
 190            }
 4191            else if (property.NameEquals(EncryptionAgent_EncryptionVersionName))
 192            {
 4193                agent.EncryptionVersion = property.Value.GetString().ToClientSideEncryptionVersion();
 194            }
 4195        }
 196        #endregion
 197    }
 198}