< Summary

Class:Azure.Storage.Blobs.Models.CustomerProvidedKey
Assembly:Azure.Storage.Blobs
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs\src\Models\CustomerProvidedKey.cs
Covered lines:10
Uncovered lines:10
Coverable lines:20
Total lines:100
Line coverage:50% (10 of 20)
Covered branches:0
Total branches:6
Branch coverage:0% (0 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_EncryptionKey()-100%100%
get_EncryptionKeyHash()-100%100%
get_EncryptionAlgorithm()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
Equals(...)-0%0%
GetHashCode()-0%100%
op_Equality(...)-0%100%
op_Inequality(...)-0%100%
Equals(...)-0%0%
ToString()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs\src\Models\CustomerProvidedKey.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Security.Cryptography;
 6
 7namespace Azure.Storage.Blobs.Models
 8{
 9    /// <summary>
 10    /// Wrapper for an encryption key to be used with client provided key server-side encryption.
 11    /// </summary>
 12    public readonly struct CustomerProvidedKey : IEquatable<CustomerProvidedKey>
 13    {
 14        /// <summary>
 15        /// Base64 encoded string of the AES256 encryption key.
 16        /// </summary>
 10817        public readonly string EncryptionKey { get; }
 18
 19        /// <summary>
 20        /// Base64 encoded string of the AES256 encryption key's SHA256 hash.
 21        /// </summary>
 15222        public readonly string EncryptionKeyHash { get; }
 23
 24        /// <summary>
 25        /// The algorithm for Azure Blob Storage to encrypt with.
 26        /// Azure Blob Storage only offers AES256 encryption.
 27        /// </summary>
 10828        public readonly EncryptionAlgorithmType EncryptionAlgorithm { get; }
 29
 30        /// <summary>
 31        /// Creates a new CustomerProvidedKey for use in server-side encryption.
 32        /// </summary>
 33        /// <param name="key">The encryption key encoded as a base64 string.</param>
 34        public CustomerProvidedKey(string key)
 35        {
 8836            EncryptionKey = key;
 8837            EncryptionAlgorithm = EncryptionAlgorithmType.Aes256;
 8838            using var sha256 = SHA256.Create();
 8839            var encodedHash = sha256.ComputeHash(Convert.FromBase64String(key));
 8840            EncryptionKeyHash = Convert.ToBase64String(encodedHash);
 17641        }
 42
 43        /// <summary>
 44        /// Creates a new CustomerProvidedKey for use in server-side encryption.
 45        /// </summary>
 46        /// <param name="key">The encryption key bytes.</param>
 17647        public CustomerProvidedKey(byte[] key) : this(Convert.ToBase64String(key)) { }
 48
 49        /// <summary>
 50        /// Checks if two CustomerProvidedKeyInfo are equal to each other.
 51        /// </summary>
 52        /// <param name="obj">The other instance to compare to.</param>
 53        public override bool Equals(object obj)
 054            => obj is CustomerProvidedKey other && Equals(other);
 55
 56        /// <summary>
 57        /// Get a hash code for the CustomerProvidedKeyInfo.
 58        /// </summary>
 59        /// <returns>Hash code for the CustomerProvidedKeyInfo.</returns>
 60        public override int GetHashCode()
 061            => EncryptionKey.GetHashCode()
 062            ^ EncryptionKeyHash.GetHashCode()
 063            ^ EncryptionAlgorithm.GetHashCode()
 64            ;
 65
 66        /// <summary>
 67        /// Check if two CustomerProvidedKeyInfo instances are equal.
 68        /// </summary>
 69        /// <param name="left">The first instance to compare.</param>
 70        /// <param name="right">The second instance to compare.</param>
 71        /// <returns>True if they're equal, false otherwise.</returns>
 072        public static bool operator ==(CustomerProvidedKey left, CustomerProvidedKey right) => left.Equals(right);
 73
 74        /// <summary>
 75        /// Check if two CustomerProvidedKeyInfo instances are not equal.
 76        /// </summary>
 77        /// <param name="left">The first instance to compare.</param>
 78        /// <param name="right">The second instance to compare.</param>
 79        /// <returns>True if they're not equal, false otherwise.</returns>
 080        public static bool operator !=(CustomerProvidedKey left, CustomerProvidedKey right) => !(left == right);
 81
 82        /// <summary>
 83        /// Checks if two CustomerProvidedKeyInfo are equal to each other.
 84        /// </summary>
 85        /// <param name="other">The other instance to compare to.</param>
 86        /// <returns></returns>
 87        public bool Equals(CustomerProvidedKey other)
 088         => EncryptionKey == other.EncryptionKey
 089            && EncryptionKeyHash == other.EncryptionKeyHash
 090            && EncryptionAlgorithm == other.EncryptionAlgorithm
 91            ;
 92
 93        /// <summary>
 94        /// ToString
 95        /// </summary>
 96        /// <returns>string</returns>
 97        public override string ToString()
 098            => $"[{nameof(CustomerProvidedKey)}:{nameof(EncryptionKey)}={EncryptionKey};{nameof(EncryptionKeyHash)}={Enc
 99    }
 100}