forked from NTDLS/NSWFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSWFL_KeyGeneration.Cpp
97 lines (76 loc) · 2.89 KB
/
NSWFL_KeyGeneration.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
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright © NetworkDLS 2023, All rights reserved
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _NSWFL_KEYGENERATIION_CPP_
#define _NSWFL_KEYGENERATIION_CPP_
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "NSWFL.H"
#ifdef _USE_GLOBAL_MEMPOOL
extern NSWFL::Memory::MemoryPool *pMem; //pMem must be defined and initalized elsewhere.
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace NSWFL {
namespace KeyGeneration {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned long GenerateSeed(void)
{
srand(GetTickCount());
return rand();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool GenerateKey(int iNewKeyLen, unsigned long lSeed, int iAttribs, char *sOutKey)
{
bool ValidValue = false;
int iWPos = 0;
unsigned char Random = 0;
unsigned char LastValue = 0;
if (!(iAttribs &GKNUMBERS) && !(iAttribs &GKUPPER_AZ)
&& !(iAttribs &GKLOWER_AZ) && !(iAttribs &GKBROADKEY))
{
return false;
}
srand(lSeed);
while (iWPos < iNewKeyLen)
{
Random = (unsigned char)rand() % 256;
if (iAttribs &GKNUMBERS) // 0 - 9
if ((Random < 57) && (Random > 48))
ValidValue = true;
if (iAttribs &GKUPPER_AZ) // A - Z
if ((Random < 90) && (Random > 65))
ValidValue = true;
if (iAttribs &GKLOWER_AZ) // a - z
if ((Random < 122) && (Random > 97))
ValidValue = true;
if (iAttribs &GKBROADKEY) // Broad Key
ValidValue = true;
if (Random == 0) ValidValue = false; // Null
if (Random == 10) ValidValue = false; // lf
if (Random == 13) ValidValue = false; // cr
if (iAttribs &GKNONREPETITION)
{
if (Random == LastValue)
{
ValidValue = false;
}
}
if (ValidValue)
{
sOutKey[iWPos] = Random;
LastValue = Random;
iWPos++;
}
ValidValue = false;
}
sOutKey[iWPos] = '\0';
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} //namespace::KeyGeneration
} //namespace::NSWFL
#endif