Repository navigation
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
53 lines (49 loc) · 1.55 KB
/
Copy pathStringExtensions.cs
File metadata and controls
53 lines (49 loc) · 1.55 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
53
namespace HIBP.Helpers
{
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// String extensions.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Gets first 5 characters of a string.
/// </summary>
/// <param name="s">The s.</param>
/// <returns>
/// The first 5 characters of a string.
/// </returns>
internal static string First5(this string s)
{
return s.Substring(0, 5);
}
/// <summary>
/// Converts to a boolean string.
/// </summary>
/// <param name="b">if set to <c>true</c> [b].</param>
/// <returns>A string representing the boolean value.</returns>
internal static string ToBooleanString(this bool b)
{
return b == true ? "true" : "false";
}
/// <summary>
/// Converts to sha1.
/// </summary>
/// <param name="s">The s.</param>
/// <returns>A sha1 hashed string.</returns>
public static string ToSHA1(this string s)
{
using (var provider = new SHA1Managed())
{
var hash = provider.ComputeHash(Encoding.UTF8.GetBytes(s));
var sb = new StringBuilder(hash.Length * 2);
foreach (var @byte in hash)
{
sb.Append(@byte.ToString("X2"));
}
return sb.ToString();
}
}
}
}