-
Notifications
You must be signed in to change notification settings - Fork 0
/
CyptoHashAlgorithem.cs
53 lines (45 loc) · 1.22 KB
/
CyptoHashAlgorithem.cs
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
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
/*
* Hashing is an important technique which converts any object into
* an integer of a given range. Hashing is the key idea
* behind Hash Maps which provides searching in any dataset in O(1) time complexity.
*
*
* Hash of a string
For a string of length N, a strong hash function is defined as:
H(S)=(S[0]+S[1]∗P+S[2]∗P2+...+S[N−1]∗PN−1)modM
H(S)=(N−1∑i=0 S[i]∗Pi)modM
where:
P is a prime number (say 3)
S[i] is the ascii value of the character at index i of S (String)
M is the upper limit/ range of the hash function
*/
namespace ConsoleApp13
{
class Program
{
public static void Main()
{
string str = "HarshalRaverkar";
var t = GetHashString(str);
Console.WriteLine(t);
}
private static string GetHashString(string str)
{
StringBuilder sb = new StringBuilder();
foreach(var t in GetHash(str))
{
sb.Append(t.ToString("X2"));
}
return sb.ToString();
}
private static byte[] GetHash(string str)
{
HashAlgorithm algorithm = SHA256.Create();
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(str));
}
}
}