microsoft/healthcare-shared-components
Publicmirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable
src/Microsoft.Health.Core/Extensions/StringExtensions.cs
31lines · modecode
| 1 | // ------------------------------------------------------------------------------------------------- |
| 2 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | // ------------------------------------------------------------------------------------------------- |
| 5 | |
| 6 | using System; |
| 7 | using System.Security.Cryptography; |
| 8 | using System.Text; |
| 9 | using EnsureThat; |
| 10 | |
| 11 | namespace Microsoft.Health.Core.Extensions |
| 12 | { |
| 13 | public static class StringExtensions |
| 14 | { |
| 15 | /// <summary> |
| 16 | /// Computes SHA256 hash based of <paramref name="data"/>. |
| 17 | /// </summary> |
| 18 | /// <param name="data">The data to compute hash on.</param> |
| 19 | /// <returns>The computed hash string.</returns> |
| 20 | public static string ComputeHash(this string data) |
| 21 | { |
| 22 | EnsureArg.IsNotNull(data, nameof(data)); |
| 23 | |
| 24 | using (var sha256 = new SHA256Managed()) |
| 25 | { |
| 26 | var hashed = sha256.ComputeHash(Encoding.UTF8.GetBytes(data)); |
| 27 | return BitConverter.ToString(hashed).Replace("-", string.Empty, StringComparison.Ordinal); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |