forked from technificentConsulting/TS4-SimRipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFNV.cs
106 lines (96 loc) · 3.42 KB
/
FNV.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* TS4 MorphMaker, a tool for creating custom content for The Sims 4,
Copyright (C) 2014 C. Marinetti
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The author may be contacted at modthesims.info, username cmarNYC. */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TS4SimRipper
{
public static class FNVhash
{
private static uint FNV_PRIME_32 = 16777619;
private static uint FNV_OFFSET_32 = 2166136261;
private static ulong FNV_PRIME_64 = 1099511628211;
private static ulong FNV_OFFSET_64 = 14695981039346656037;
public static ushort FNV16(string str)
{
uint hash = FNV_OFFSET_32;
string s = str.ToLower();
byte[] ASCII = Encoding.ASCII.GetBytes(s);
foreach (byte b in ASCII)
{
unchecked { hash = hash * FNV_PRIME_32; }
hash = hash ^ b;
}
uint shifted = hash >> 16;
hash = hash ^ shifted;
hash = hash & 65535;
return (ushort)hash;
}
public static uint FNV24(string str)
{
uint hash = FNV_OFFSET_32;
string s = str.ToLower();
byte[] ASCII = Encoding.ASCII.GetBytes(s);
foreach (byte b in ASCII)
{
unchecked { hash = hash * FNV_PRIME_32; }
hash = hash ^ b;
}
uint shifted = hash >> 24;
hash = hash ^ shifted;
hash = hash & 16777215;
return hash;
}
public static uint FNV32(string str)
{
uint hash = FNV_OFFSET_32;
string s = str.ToLower();
byte[] ASCII = Encoding.ASCII.GetBytes(s);
foreach (byte b in ASCII)
{
unchecked { hash = hash * FNV_PRIME_32; }
hash = hash ^ b;
}
return hash;
}
public static ulong FNV64(string str)
{
ulong hash = FNV_OFFSET_64;
string s = str.ToLower();
byte[] ASCII = Encoding.ASCII.GetBytes(s);
foreach (byte b in ASCII)
{
unchecked { hash = hash * FNV_PRIME_64; }
hash = hash ^ b;
}
return hash;
}
public static String FormatFNV(uint Hash)
{
return Convert.ToString(Hash, 16).ToUpper().PadLeft(8, '0');
}
public static String FormatFNV(ulong Hash)
{
ulong tmp = Hash;
uint low64, hi64;
unchecked
{
low64 = (uint)tmp;
hi64 = (uint)(tmp >> 32);
}
return Convert.ToString(hi64, 16).ToUpper().PadLeft(8, '0') + Convert.ToString(low64, 16).ToUpper().PadLeft(8, '0');
}
}
}