-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
174 lines (159 loc) · 4.99 KB
/
hash.cpp
File metadata and controls
174 lines (159 loc) · 4.99 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// hash.c
// test
//
// Created by laomi on 2018/3/24.
// Copyright © 2018年 LAOMI. All rights reserved.
//
#include <iostream>
using namespace std;
#include "hash.h"
#include "string.h"
#include <string>
#include <sstream>
unsigned long rest;
void Hash::get_smtkey_phrase(const unsigned char lock_id[4], const unsigned char mobile_no[11], const unsigned char date[4], const unsigned char time[2], int duration, unsigned char smtkey_phrase[8])
{
unsigned char buf[24];
buf[ 0] = 'X';
buf[ 1] = 'H';
buf[ 2] = 'Z';
buf[ 3] = duration;
buf[ 4] = 'n';
buf[ 5] = 'a';
buf[ 6] = 'C';
buf[ 7] = time[1];
buf[8] = lock_id[0];
buf[9] = lock_id[1];
buf[10] = lock_id[2];
buf[11] = lock_id[3];
buf[12] = 'L';
buf[13] = 'j';
buf[14] = (mobile_no[0] << 6) | (mobile_no[1] << 4) | mobile_no[2];
buf[15] = (mobile_no[3] << 4) | mobile_no[4];
buf[16] = (mobile_no[5] << 4) | mobile_no[6];
buf[17] = (mobile_no[7] << 4) | mobile_no[8];
buf[18] = (mobile_no[9] << 4) | mobile_no[10];
buf[19] = date[0];
buf[20] = date[1];
buf[21] = date[2];
buf[22] = date[3];
buf[23] = time[0];
printf("\n------buf%s",buf);
lh_strhash(buf, 24, smtkey_phrase);
// return 0;
}
void Hash::lh_strhash(const unsigned char buf[], unsigned char len, unsigned char result[8])
{
unsigned long ret = 0;
unsigned long n;
unsigned long v;
unsigned int r;
unsigned char *c = (unsigned char *)buf;
n = 0x100;
unsigned char i;
for (i=0; i<len; i++)
{
v = n | (*c);
n += 0x100;
r = (unsigned int)((v >> 2) ^ v) & 0x0f;
ret = (ret << r) | (ret >> (32 - r));
ret &= 0x0FFFFFFFFULL;
ret ^= v * v;
c++;
}
ret = ((ret >> 16) ^ ret);
result[0] = ret & 0xff;
result[1] = (ret >> 8) & 0xff;
result[2] = (ret >> 16) & 0xff;
result[3] = ret >> 24;
result[4] = (ret >> 1) & 0xff;
result[5] = (ret >> 7) & 0xff;
result[6] = (ret >> 11) & 0xff;
result[7] = (ret >> 19) & 0xff;
printf("\n------result:%s",result);
// return 0;
}
string Hash::get_password(string lock_id_s, string mobile_no_s, string date_s, string time_s, int duration)
//int get_password(const unsigned char lock_id[4], const unsigned char mobile_no[11], const unsigned char date[4], const unsigned char time[2], int duration)
{
const char* lock_id_c = lock_id_s.c_str();
const char* mobile_no_c = mobile_no_s.c_str();
const char* date_c = date_s.c_str();
const char* time_c = time_s.c_str();
unsigned char phrase[8];
printf("get_password\n");
printf("lock_id:%s\n",lock_id_c);
printf("mobile_no:%s\n",mobile_no_c);
printf("date:%s\n",date_c);
printf("time:%s\n",time_c);
printf("=======================\n");
unsigned char lock_id[4+1];
unsigned char mobile_no[11+1];
unsigned char date[4+1];
unsigned char time[2+1];
int i;
for(i=0;i<strlen(lock_id_c);i++){
char tmp = *(lock_id_c+i);
printf("==== %c\n", tmp);
lock_id[i] = tmp;
}
lock_id[strlen(lock_id_c)]='\0';
for(i=0;i<strlen(mobile_no_c);i++){
char tmp = *(mobile_no_c+i);
printf("~~~~ %c\n", tmp);
mobile_no[i] = tmp;
}
mobile_no[strlen(mobile_no_c)]='\0';
for(i=0;i<strlen(date_c);i++){
char tmp = *(date_c+i);
date[i] = tmp;
}
date[strlen(date_c)]='\0';
for(i=0;i<strlen(time_c);i++){
char tmp = *(time_c+i);
time[i] = tmp;
}
time[strlen(time_c)]='\0';
// strncpy(lock_id, lock_id_c, strlen(lock_id) + 1);
// strncpy(mobile_no, mobile_no_c, strlen(mobile_no) + 1);
// strncpy(date, date_c, strlen(date_c) + 1);
// strncpy(time, time_c, strlen(time_c) + 1);
printf("lock_id:%s\n",lock_id);
printf("mobile_no:%s\n",mobile_no);
printf("date:%s\n",date);
printf("time:%s\n",time);
get_smtkey_phrase(lock_id, mobile_no, date, time, duration, phrase);
// password[0] = (unsigned short)(phrase[0]^phrase[5]) % 100u;
// password[1] = (unsigned short)(~phrase[1]^phrase[6]) % 100u;
// password[2] = (unsigned short)~(phrase[2]^phrase[4]) % 100u;
// password[3] = (unsigned short)(phrase[3]^~phrase[7]) % 100u;
//
unsigned short p1,p2,p3,p4;
p1 = (unsigned short)(phrase[0]^phrase[5]) % 100u;
p2 = (unsigned short)(~phrase[1]^phrase[6]) % 100u;
p3 = (unsigned short)~(phrase[2]^phrase[4]) % 100u;
p4 = (unsigned short)(phrase[3]^~phrase[7]) % 100u;
// //转成long丢出来;
rest = p1*1000000+p2*10000+p3*100+p1;
// return rest;
printf("\npass:%ld",rest);
stringstream ss;
ss << rest;
return ss.str();
//return 0;
// char chatTotal[13];
// char dest[] = {p1,p2,p3,p4};
// int i= 0;
// char temp[3];
// while (i<4) {
// sprintf(temp, "%d", dest[i]);
// strcat(chatTotal,temp);
// i++;
// }
// strcpy(g_s,chatTotal);
// return (char*)g_s;
}
int Hash::test(const int a,const int b,const int c){
return a+b+c;
}