-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSecurityPassword.h
165 lines (139 loc) · 3.29 KB
/
SecurityPassword.h
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
/*
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
*
* The Original Code was created by Vladimir Tsvigun for IBPhoenix.
*
* Copyright (c) 2004 Vladimir Tsvigun
* All Rights Reserved.
*/
//
// CSecurityPassword.h: interface for the CSecurityPassword class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(_CSecurityPassword_H_)
#define _CSecurityPassword_H_
namespace classSecurityPassword {
class CShift
{
char segment[6];
int off;
public:
CShift()
{
memset ( segment, 0, sizeof(segment) );
off = 0;
}
void operator= ( char *&seg )
{
memcpy ( segment, seg, sizeof(segment) );
++seg += sizeof(segment);
}
char operator++ ( int pos )
{
return segment[off++ % 6];
}
};
class CSecurityPassword
{
char securityKey[40];
CShift shift[4];
int rep;
char chKey;
public:
CSecurityPassword()
{
memset ( securityKey, 0, sizeof(securityKey) );
rep = sizeof(shift)/sizeof(shift[0]);
chKey = 0;
}
void buildKey( char * pass )
{
int len = (int)strlen(pass);
if ( !len )
return;
unsigned char p=0;
char * beg = securityKey;
char * end = beg + sizeof(securityKey);
int off = 0;
while ( beg < end )
{
char * ch1 = pass + off % len;
char * ch2 = pass + off++ % len;
*beg++ = (char)(*ch1 * (p + (0x11 * (*ch1 + off))) + *ch2);
}
}
void initShifts()
{
char * beg = securityKey + 3;
int i = 0;
while ( i < rep )
shift[i++] = beg;
}
char get()
{
char ch = 0;
int i = 0;
while ( i < rep )
ch ^= shift[i++]++;
return ch;
}
void make ( char *buf, int len )
{
for ( int i = 0 ; i < len ; i++ )
*buf++ ^= get();
}
void encode ( char *password, char *passkey )
{
char *pt = passkey;
int len = (int)strlen(password);
buildKey( password );
initShifts();
memcpy ( pt, securityKey, sizeof(securityKey) );
pt += sizeof(securityKey);
memcpy ( pt, password, len );
make ( pt, len );
pt[len]=0;
// convert to hex string
len += sizeof(securityKey);
char *address = passkey + (len << 1);
char *end = (char *)passkey + len - 1;
*address-- = 0;
while( len-- )
*address-- = (((*end >> 4) & 0x0F) + 'A'),
*address-- = ((*end-- & 0x0F) + 'A');
}
void decode ( char *passkey, char *password )
{
if ( !*passkey )
return;
int lenkey = (int)strlen ( passkey );
if ( lenkey % 2 )
return;
lenkey /=2;
int len = lenkey - sizeof(securityKey);
char *beg = passkey;
char *next = passkey;
while( lenkey-- )
*beg = *next++ - 'A',*beg++ += (*next++ - 'A') << 4;
char *pt = passkey;
memcpy ( securityKey, passkey, sizeof(securityKey) );
pt += sizeof(securityKey);
initShifts();
make ( pt, len );
memcpy ( password, pt, len );
password[len]=0;
}
};
}; // end namespace classSecurityPassword
#endif // !defined(_CSecurityPassword_H_)