forked from DuendeArchive/IdentityModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
52 lines (45 loc) · 1.52 KB
/
Copy pathStringExtensions.cs
File metadata and controls
52 lines (45 loc) · 1.52 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using IdentityModel.Internal;
using System;
using System.Security.Cryptography;
using System.Text;
namespace IdentityModel
{
/// <summary>
/// Extensions for strings
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Creates a SHA256 hash of the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A hash</returns>
public static string ToSha256(this string input)
{
if (input.IsMissing()) return string.Empty;
using (var sha = SHA256.Create())
{
var bytes = Encoding.UTF8.GetBytes(input);
var hash = sha.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
}
/// <summary>
/// Creates a SHA512 hash of the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A hash</returns>
public static string ToSha512(this string input)
{
if (input.IsMissing()) return string.Empty;
using (var sha = SHA512.Create())
{
var bytes = Encoding.UTF8.GetBytes(input);
var hash = sha.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
}
}
}