|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Buffers; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Security.Cryptography; |
| 8 | +using System.Text; |
| 9 | +using SixLabors.ImageSharp.Web.Caching; |
| 10 | + |
| 11 | +namespace SixLabors.ImageSharp.Web |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Provides methods to compute a Hash-based Message Authentication Code (HMAC). |
| 15 | + /// </summary> |
| 16 | + public static class HMACUtilities |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// The command used by image requests for transporting Hash-based Message Authentication Code (HMAC) tokens. |
| 20 | + /// </summary> |
| 21 | + public const string TokenCommand = "hmac"; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Computes a Hash-based Message Authentication Code (HMAC) by using the SHA256 hash function. |
| 25 | + /// </summary> |
| 26 | + /// <param name="value">The value to hash</param> |
| 27 | + /// <param name="secret"> |
| 28 | + /// The secret key for <see cref="HMACSHA256"/> encryption. |
| 29 | + /// The key can be any length. However, the recommended size is 64 bytes. |
| 30 | + /// </param> |
| 31 | + /// <returns>The hashed <see cref="string"/>.</returns> |
| 32 | + public static unsafe string ComputeHMACSHA256(string value, byte[] secret) |
| 33 | + { |
| 34 | + // TODO: In .NET 6 we can use single instance versions |
| 35 | + using var hmac = new HMACSHA256(secret); |
| 36 | + return CreateHMAC(value, hmac); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Computes a Hash-based Message Authentication Code (HMAC) by using the SHA384 hash function. |
| 41 | + /// </summary> |
| 42 | + /// <param name="value">The value to hash</param> |
| 43 | + /// <param name="secret"> |
| 44 | + /// The secret key for <see cref="HMACSHA256"/> encryption. |
| 45 | + /// The key can be any length. However, the recommended size is 128 bytes. |
| 46 | + /// </param> |
| 47 | + /// <returns>The hashed <see cref="string"/>.</returns> |
| 48 | + public static unsafe string ComputeHMACSHA384(string value, byte[] secret) |
| 49 | + { |
| 50 | + using var hmac = new HMACSHA384(secret); |
| 51 | + return CreateHMAC(value, hmac); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Computes a Hash-based Message Authentication Code (HMAC) by using the SHA512 hash function. |
| 56 | + /// </summary> |
| 57 | + /// <param name="value">The value to hash</param> |
| 58 | + /// <param name="secret"> |
| 59 | + /// The secret key for <see cref="HMACSHA256"/> encryption. |
| 60 | + /// The key can be any length. However, the recommended size is 128 bytes. |
| 61 | + /// </param> |
| 62 | + /// <returns>The hashed <see cref="string"/>.</returns> |
| 63 | + public static unsafe string ComputeHMACSHA512(string value, byte[] secret) |
| 64 | + { |
| 65 | + using var hmac = new HMACSHA512(secret); |
| 66 | + return CreateHMAC(value, hmac); |
| 67 | + } |
| 68 | + |
| 69 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 70 | + private static unsafe string CreateHMAC(string value, HMAC hmac) |
| 71 | + { |
| 72 | + int byteCount = Encoding.ASCII.GetByteCount(value); |
| 73 | + byte[] buffer = null; |
| 74 | + |
| 75 | + try |
| 76 | + { |
| 77 | + // Allocating a buffer from the pool is ~27% slower than stackalloc so use that for short strings |
| 78 | + Span<byte> bytes = byteCount <= 128 |
| 79 | + ? stackalloc byte[byteCount] |
| 80 | + : (buffer = ArrayPool<byte>.Shared.Rent(byteCount)).AsSpan(0, byteCount); |
| 81 | + |
| 82 | + Encoding.ASCII.GetBytes(value, bytes); |
| 83 | + |
| 84 | + // Safe to always stackalloc here. We max out at 64 bytes. |
| 85 | + Span<byte> hash = stackalloc byte[hmac.HashSize / 8]; |
| 86 | + hmac.TryComputeHash(bytes, hash, out int _); |
| 87 | + |
| 88 | + // Finally encode the hash to make it web safe. |
| 89 | + return HexEncoder.Encode(hash); |
| 90 | + } |
| 91 | + finally |
| 92 | + { |
| 93 | + if (buffer is not null) |
| 94 | + { |
| 95 | + ArrayPool<byte>.Shared.Return(buffer); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments