| | 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.Data.Tables |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A <see cref="TableSharedKeyCredential"/> is a credential backed by |
| | 13 | | /// a Storage Account's name and one of its access keys. |
| | 14 | | /// </summary> |
| | 15 | | public class TableSharedKeyCredential |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Gets the name of the Storage Account. |
| | 19 | | /// </summary> |
| 8056 | 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 | | { |
| 4048 | 32 | | get => Volatile.Read(ref _accountKeyValue); |
| 432 | 33 | | set => Volatile.Write(ref _accountKeyValue, value); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Initializes a new instance of the |
| | 38 | | /// <see cref="TableSharedKeyCredential"/> class. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="accountName">The name of the Storage Account.</param> |
| | 41 | | /// <param name="accountKey">A Storage Account access key.</param> |
| 432 | 42 | | public TableSharedKeyCredential( |
| 432 | 43 | | string accountName, |
| 432 | 44 | | string accountKey) |
| | 45 | | { |
| 432 | 46 | | AccountName = accountName; |
| 432 | 47 | | SetAccountKey(accountKey); |
| 432 | 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) => |
| 432 | 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) => |
| 4048 | 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 | | internal static string ComputeSasSignature(TableSharedKeyCredential credential, string message) => |
| 4048 | 76 | | credential.ComputeHMACSHA256(message); |
| | 77 | | } |
| | 78 | | } |