< Summary

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

Metrics

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

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        {
 052            json.WriteString(nameof(data.EncryptionMode), data.EncryptionMode);
 53
 054            json.WriteStartObject(nameof(data.WrappedContentKey));
 055            WriteWrappedKey(json, data.WrappedContentKey);
 056            json.WriteEndObject();
 57
 058            json.WriteStartObject(nameof(data.EncryptionAgent));
 059            WriteEncryptionAgent(json, data.EncryptionAgent);
 060            json.WriteEndObject();
 61
 062            json.WriteString(nameof(data.ContentEncryptionIV), Convert.ToBase64String(data.ContentEncryptionIV));
 63
 064            json.WriteStartObject(nameof(data.KeyWrappingMetadata));
 065            WriteDictionary(json, data.KeyWrappingMetadata);
 066            json.WriteEndObject();
 067        }
 68
 69        private static void WriteWrappedKey(Utf8JsonWriter json, KeyEnvelope key)
 70        {
 071            json.WriteString(nameof(key.KeyId), key.KeyId);
 072            json.WriteString(nameof(key.EncryptedKey), Convert.ToBase64String(key.EncryptedKey));
 073            json.WriteString(nameof(key.Algorithm), key.Algorithm);
 074        }
 75
 76        private static void WriteEncryptionAgent(Utf8JsonWriter json, EncryptionAgent encryptionAgent)
 77        {
 078            json.WriteString(EncryptionAgent_EncryptionVersionName, encryptionAgent.EncryptionVersion.Serialize());
 079            json.WriteString(nameof(encryptionAgent.EncryptionAlgorithm), encryptionAgent.EncryptionAlgorithm.ToString()
 080        }
 81
 82        private static void WriteDictionary(Utf8JsonWriter json, IDictionary<string, string> dictionary)
 83        {
 084            foreach (var entry in dictionary)
 85            {
 086                json.WriteString(entry.Key, entry.Value);
 87            }
 088        }
 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        {
 0122            var data = new EncryptionData();
 0123            foreach (var property in root.EnumerateObject())
 124            {
 0125                ReadPropertyValue(data, property);
 126            }
 0127            return data;
 128        }
 129
 130        private static void ReadPropertyValue(EncryptionData data, JsonProperty property)
 131        {
 0132            if (property.NameEquals(nameof(data.EncryptionMode)))
 133            {
 0134                data.EncryptionMode = property.Value.GetString();
 135            }
 0136            else if (property.NameEquals(nameof(data.WrappedContentKey)))
 137            {
 0138                var key = new KeyEnvelope();
 0139                foreach (var subProperty in property.Value.EnumerateObject())
 140                {
 0141                    ReadPropertyValue(key, subProperty);
 142                }
 0143                data.WrappedContentKey = key;
 144            }
 0145            else if (property.NameEquals(nameof(data.EncryptionAgent)))
 146            {
 0147                var agent = new EncryptionAgent();
 0148                foreach (var subProperty in property.Value.EnumerateObject())
 149                {
 0150                    ReadPropertyValue(agent, subProperty);
 151                }
 0152                data.EncryptionAgent = agent;
 153            }
 0154            else if (property.NameEquals(nameof(data.ContentEncryptionIV)))
 155            {
 0156                data.ContentEncryptionIV = Convert.FromBase64String(property.Value.GetString());
 157            }
 0158            else if (property.NameEquals(nameof(data.KeyWrappingMetadata)))
 159            {
 0160                var metadata = new Dictionary<string, string>();
 0161                foreach (var entry in property.Value.EnumerateObject())
 162                {
 0163                    metadata.Add(entry.Name, entry.Value.GetString());
 164                }
 0165                data.KeyWrappingMetadata = metadata;
 166            }
 0167        }
 168
 169        private static void ReadPropertyValue(KeyEnvelope key, JsonProperty property)
 170        {
 0171            if (property.NameEquals(nameof(key.Algorithm)))
 172            {
 0173                key.Algorithm = property.Value.GetString();
 174            }
 0175            else if (property.NameEquals(nameof(key.EncryptedKey)))
 176            {
 0177                key.EncryptedKey = Convert.FromBase64String(property.Value.GetString());
 178            }
 0179            else if (property.NameEquals(nameof(key.KeyId)))
 180            {
 0181                key.KeyId = property.Value.GetString();
 182            }
 0183        }
 184
 185        private static void ReadPropertyValue(EncryptionAgent agent, JsonProperty property)
 186        {
 0187            if (property.NameEquals(nameof(agent.EncryptionAlgorithm)))
 188            {
 0189                agent.EncryptionAlgorithm = new ClientSideEncryptionAlgorithm(property.Value.GetString());
 190            }
 0191            else if (property.NameEquals(EncryptionAgent_EncryptionVersionName))
 192            {
 0193                agent.EncryptionVersion = property.Value.GetString().ToClientSideEncryptionVersion();
 194            }
 0195        }
 196        #endregion
 197    }
 198}