-
Notifications
You must be signed in to change notification settings - Fork 0
/
uid_to_hash_ver0.1.cpp
55 lines (48 loc) · 1.14 KB
/
uid_to_hash_ver0.1.cpp
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
#include <iostream>
#include <cstring>
using namespace std;
unsigned long Crc32Table[256];
int length(int num) {
int len = 1;
while (num / 10 != 0) {
++len;
num /= 10;
}
return len;
}
char getIthChar(int n,int i){
return to_string(n)[i];
}
void GetCrc32Table() {
unsigned long Crc=0;
int i=0, j=0;
for (i = 0; i < 256; ++i) {
Crc = i;
for (j = 8; j > 0; --j) {
if ((Crc & 1) == (unsigned int)1) {
Crc =( (Crc >> 1) ^ (0xedb88320));
} else {
Crc = Crc>>1;
}
}
Crc32Table[i] = Crc;
}
}
unsigned long GetCrc32Str(unsigned int n) {
unsigned long value = 0xffffffff;
int len = length(n);
for (int i = 0; i < len; i++) {
value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ (getIthChar(n,i))];
}
return value ^ 0xffffffff;
}
int main() {
memset(Crc32Table, 0, sizeof(Crc32Table));
GetCrc32Table();
unsigned int n;
cout<<"输入用户uid:\n";
cin>>n;
cout<<"用户 uid:"<<n<<" 的hashID为:\n"<<hex<<GetCrc32Str(n)<<endl;
//system("pause");
return 0;
}