microsoft/healthcare-shared-components

Public

mirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.0-1.0.79

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
6using System;
7using System.Security.Cryptography;
8using System.Text;
9using EnsureThat;
10
11namespace 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