-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathCryptographyExtensions.cs
More file actions
34 lines (30 loc) · 1.2 KB
/
CryptographyExtensions.cs
File metadata and controls
34 lines (30 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace Microsoft.OpenApi.OData.Common
{
internal static class CryptographyExtensions
{
private static readonly ThreadLocal<SHA256> hasher = new (SHA256.Create);
/// <summary>
/// Calculates the SHA256 hash for the given string.
/// </summary>
/// <returns>A 64 char long hash.</returns>
public static string GetHashSHA256(this string input)
{
Utils.CheckArgumentNull(input, nameof(input));
var inputBytes = Encoding.UTF8.GetBytes(input);
if (hasher.Value?.ComputeHash(inputBytes) is not {} hashBytes) return string.Empty;
var hash = new StringBuilder();
foreach (var b in hashBytes)
{
hash.Append(string.Format("{0:x2}", b));
}
return hash.ToString();
}
}
}