| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Security.Cryptography; |
| | 6 | |
|
| | 7 | | namespace 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> |
| 108 | 17 | | public readonly string EncryptionKey { get; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Base64 encoded string of the AES256 encryption key's SHA256 hash. |
| | 21 | | /// </summary> |
| 152 | 22 | | 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> |
| 108 | 28 | | 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 | | { |
| 88 | 36 | | EncryptionKey = key; |
| 88 | 37 | | EncryptionAlgorithm = EncryptionAlgorithmType.Aes256; |
| 88 | 38 | | using var sha256 = SHA256.Create(); |
| 88 | 39 | | var encodedHash = sha256.ComputeHash(Convert.FromBase64String(key)); |
| 88 | 40 | | EncryptionKeyHash = Convert.ToBase64String(encodedHash); |
| 176 | 41 | | } |
| | 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> |
| 176 | 47 | | 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) |
| 0 | 54 | | => 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() |
| 0 | 61 | | => EncryptionKey.GetHashCode() |
| 0 | 62 | | ^ EncryptionKeyHash.GetHashCode() |
| 0 | 63 | | ^ 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> |
| 0 | 72 | | 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> |
| 0 | 80 | | 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) |
| 0 | 88 | | => EncryptionKey == other.EncryptionKey |
| 0 | 89 | | && EncryptionKeyHash == other.EncryptionKeyHash |
| 0 | 90 | | && EncryptionAlgorithm == other.EncryptionAlgorithm |
| | 91 | | ; |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// ToString |
| | 95 | | /// </summary> |
| | 96 | | /// <returns>string</returns> |
| | 97 | | public override string ToString() |
| 0 | 98 | | => $"[{nameof(CustomerProvidedKey)}:{nameof(EncryptionKey)}={EncryptionKey};{nameof(EncryptionKeyHash)}={Enc |
| | 99 | | } |
| | 100 | | } |