| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Security.Cryptography; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A <see cref="StorageSharedKeyCredential"/> is a credential backed by |
| | 13 | | /// a Storage Account's name and one of its access keys. |
| | 14 | | /// </summary> |
| | 15 | | public class StorageSharedKeyCredential |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Gets the name of the Storage Account. |
| | 19 | | /// </summary> |
| 80708 | 20 | | public string AccountName { get; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// The value of a Storage Account access key. |
| | 24 | | /// </summary> |
| | 25 | | private byte[] _accountKeyValue; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets the value of a Storage Account access key. |
| | 29 | | /// </summary> |
| | 30 | | private byte[] AccountKeyValue |
| | 31 | | { |
| 40606 | 32 | | get => Volatile.Read(ref _accountKeyValue); |
| 8357 | 33 | | set => Volatile.Write(ref _accountKeyValue, value); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Initializes a new instance of the |
| | 38 | | /// <see cref="StorageSharedKeyCredential"/> class. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="accountName">The name of the Storage Account.</param> |
| | 41 | | /// <param name="accountKey">A Storage Account access key.</param> |
| 8341 | 42 | | public StorageSharedKeyCredential( |
| 8341 | 43 | | string accountName, |
| 8341 | 44 | | string accountKey) |
| | 45 | | { |
| 8341 | 46 | | AccountName = accountName; |
| 8341 | 47 | | SetAccountKey(accountKey); |
| 8341 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Update the Storage Account's access key. This intended to be used |
| | 52 | | /// when you've regenerated your Storage Account's access keys and want |
| | 53 | | /// to update long lived clients. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="accountKey">A Storage Account access key.</param> |
| | 56 | | public void SetAccountKey(string accountKey) => |
| 8357 | 57 | | AccountKeyValue = Convert.FromBase64String(accountKey); |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Generates a base-64 hash signature string for an HTTP request or |
| | 61 | | /// for a SAS. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="message">The message to sign.</param> |
| | 64 | | /// <returns>The signed message.</returns> |
| | 65 | | internal string ComputeHMACSHA256(string message) => |
| 40536 | 66 | | Convert.ToBase64String(new HMACSHA256(AccountKeyValue).ComputeHash(Encoding.UTF8.GetBytes(message))); |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Generates a base-64 hash signature string for an HTTP request or |
| | 70 | | /// for a SAS. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="credential">The credential.</param> |
| | 73 | | /// <param name="message">The message to sign.</param> |
| | 74 | | /// <returns>The signed message.</returns> |
| | 75 | | protected static string ComputeSasSignature(StorageSharedKeyCredential credential, string message) => |
| 40404 | 76 | | credential.ComputeHMACSHA256(message); |
| | 77 | | } |
| | 78 | | } |