-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdgp.cpp
More file actions
201 lines (179 loc) · 5.47 KB
/
dgp.cpp
File metadata and controls
201 lines (179 loc) · 5.47 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
DGP - Deterministically Generated Passwords
Filename: dgp.cpp
Description: Everything needed to make DGP work.
Author: Adam Oldham (ekcdd@gmx.com)
Copyright (c) 2020 Adam Oldham
All rights reserved.
Distributed under the MIT/X11 software license, see the accompanying
file license.txt or http://www.opensource.org/licenses/mit-license.php.
*/
#include "dgp.h"
static uint64_t next = 1;
static std::string alpha_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static std::string num_table = "0123456789";
static std::string special_table = "`~!@#$%^&*()/{[]}\\|,<.>?";
int32_t dpg_rand(uint64_t max);
std::string ToLower(std::string s);
int64_t GenerateSeed(std::string data, uint64_t len);
std::string GeneratePasswordV1(DGPOptions options);
std::string GeneratePasswordV2(DGPOptions options);
DGPOptions CreateEmptyOptions()
{
DGPOptions opts;
opts.Length = Default_Password_Length;
opts.Rounds = 1;
opts.Version = DefaultDGPVersion;
opts.MasterKey = "";
opts.MasterKeyLength = 0;
opts.Name = "";
opts.NameLength = 0;
opts.AlphaNumericOnly = false;
return opts;
}
bool DGP(DGPOptions options,std::string &out)
{
if (options.MasterKey == "")
{
return false;
}
if (options.Name == "")
{
return false;
}
if (options.Length == 0)
{
options.Length = Default_Password_Length ;
}
if (options.Rounds == 0)
{
options.Rounds = 1;
}
options.Name = ToLower(options.Name);
switch (options.Version)
{
case DGPVersion::One:
out = GeneratePasswordV1(options);
return true;
case DGPVersion::Two:
out = GeneratePasswordV2(options);
return true;
default:
return false;
}
}
std::string ToLower(std::string s)
{
std::string result = s;
std::transform(result.begin(), result.end(), result.begin(),
[](unsigned char c){ return std::tolower(c); });
return result;
}
int32_t dpg_rand(uint64_t max)
{
if (next == 0) next++; // make sure next is not zero, thanks Keith for find this bug.
next = next * (1103515245 + 97111);
uint64_t ud = static_cast<uint64_t>((next / 65536) % max);
return static_cast<int32_t>(ud);
}
int64_t GenerateSeed(std::string data, uint64_t len)
{
int64_t hash1 = (6975 << 16) + 6975;
int64_t hash2 = hash1;
for (unsigned int i = 0; i < len; i++)
{
char c = static_cast<char>(data[i]);
hash1 = ((hash1 << 5) + hash1) ^ c;
if (c == 0)
break;
hash2 = ((hash2 << 5) + hash2) ^ c;
}
int64_t r = hash1 + (hash2 * 1577836800);
return r;
}
std::string GeneratePasswordV1(DGPOptions options)
{
uint64_t li = 0;
for (unsigned int i = 0; i < options.Rounds; i++) {
uint64_t s1 = static_cast<uint64_t>(GenerateSeed(options.MasterKey, options.MasterKeyLength));
uint64_t s2 = static_cast<uint64_t>(GenerateSeed(options.Name, options.NameLength));
li += s1 + s2;
}
next = li;
char *result = new char[options.Length];
int lastNum = 0;
int lastspec = 0;
int pos1 = dpg_rand(9);
int pos2 = dpg_rand(9);
for (unsigned int i = 0; i < options.Length; i++)
{
if (lastNum == pos1) {
result[i] = num_table[static_cast<uint32_t>(dpg_rand(10))];
lastNum = 0;
}
// This *should* be else if but changing this will result in
// a differently generated password. So for compatibility
// reasons, this will be left as it is forever or until
// the algorithm is changed causing different passwords.
if (lastspec == pos2 && options.AlphaNumericOnly == false) {
result[i] = special_table[static_cast<uint32_t>(dpg_rand(24))];
lastspec = 0;
}
else {
result[i] = alpha_table[static_cast<uint32_t>(dpg_rand(52))];
lastNum++;
lastspec++;
}
}
std::string p = std::string(result, options.Length);
delete[] result;
return p;
}
std::string GeneratePasswordV2(DGPOptions options) {
uint64_t li = 0;
for (unsigned int i = 0; i < options.Rounds; i++) {
uint64_t s1 = static_cast<uint64_t>(GenerateSeed(options.MasterKey, options.MasterKeyLength));
uint64_t s2 = static_cast<uint64_t>(GenerateSeed(options.Name, options.NameLength));
li += s1 + s2;
}
next = li;
char *result = new char[options.Length];
// std::string result = "";
int typeLength = 0;
int lastType = -1;
unsigned int opt;
if (options.AlphaNumericOnly)
{
opt = 20;
}
else {
opt = 30;
}
for (unsigned int i = 0; i < options.Length; i++)
{
int charType = dpg_rand(opt);
if ( charType == lastType && typeLength >= 4)
{
while ((charType = dpg_rand(opt)) == lastType)
{
// Do nothing but update charType
}
lastType = charType;
typeLength = 0;
}
typeLength++;
lastType = charType;
if (charType < 10) {
result[i] = alpha_table[static_cast<uint32_t>(dpg_rand(52))];
}else if (charType < 20)
{
result[i] = num_table[static_cast<uint32_t>(dpg_rand(10))];
}
else {
result[i] = special_table[static_cast<uint32_t>(dpg_rand(24))];
}
}
std::string p = std::string(result, options.Length);
delete[] result;
return p;
}