Last active
December 17, 2015 19:49
-
-
Save vaiorabbit/5663123 to your computer and use it in GitHub Desktop.
FNV-1a Hash (http://isthe.com/chongo/tech/comp/fnv/) in C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; // TextReader,TextWriter,StreamReader,StreamWriter | |
using System.Text; // Encoding | |
using System.Text.RegularExpressions; // Regex, Match | |
/* | |
$ /Library/Frameworks/Mono.framework/Commands/mcs fnv32a.cs | |
$ /Library/Frameworks/Mono.framework/Commands/mono ./fnv32a.exe > tmp_cs.txt | |
*/ | |
public class Hashcode | |
{ | |
public static uint FNV1_32A_INIT = 0x811c9dc5; | |
public static uint FNV_32_PRIME = 0x01000193; | |
public static uint fnv32a( string str ) | |
{ | |
uint hval = FNV1_32A_INIT; | |
byte[] buf = Encoding.ASCII.GetBytes(str); | |
foreach ( byte b in buf ) | |
{ | |
hval ^= b; | |
hval *= FNV_32_PRIME; | |
} | |
return hval; | |
} | |
public static void Main() | |
{ | |
TextReader instrm = new StreamReader(Console.OpenStandardInput()); | |
while ( instrm.Peek() >= 0 ) | |
{ | |
string line = instrm.ReadLine(); | |
Match result = Regex.Match( line, "^\"(.*)\",$" ); | |
if ( result.Success ) | |
{ | |
string word = result.Groups[1].Value; | |
Console.WriteLine("{0} -> {1}", word, fnv32a(word)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment