-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstretch.cpp
More file actions
116 lines (91 loc) · 2.26 KB
/
stretch.cpp
File metadata and controls
116 lines (91 loc) · 2.26 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
// Copyright (c) 2012-2018 CIYAM Developers
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
//#define DEBUG
using namespace std;
const size_t num_rounds = 10000;
void harden( size_t& count, string& np, const vector< string >& table )
{
for( size_t i = 0; i < np.size( ); i++ )
count += ( size_t )np[ i ];
}
int main( int argc, char* argv[ ] )
{
if( argc < 2 )
cout << "usage: stretch [password]" << endl;
else
{
string p( argv[ 1 ] );
vector< string > table;
ifstream inpf( "data", ios::in | ios::binary );
if( !inpf )
{
cerr << "error: cannot open 'data' for input" << endl;
return 1;
}
size_t count = 0;
for( size_t i = 0; i < 255; i++ )
{
table.push_back( "" );
for( size_t j = 0; j < 255; j++ )
{
char ch;
inpf >> ch;
++count;
table.back( ) += ch;
if( inpf.eof( ) )
break;
}
if( inpf.eof( ) )
break;
}
if( count != 65025 )
{
cerr << "error: unexpected data size: " << count << endl;
return 1;
}
count = 0;
for( string::size_type i = 0; i < p.length( ); i++ )
count += ( int )p[ i ];
size_t modulus = count % 255;
#ifdef DEBUG
cout << "modulus = " << modulus << endl;
#endif
string rp( p );
reverse( p.begin( ), p.end( ) );
string np;
count = 0;
for( size_t i = 0; i < table[ modulus ].size( ); i++ )
{
char ch = table[ modulus ][ i ];
switch( ch )
{
case '0':
case '1':
np += ch;
break;
case '2':
np += p;
break;
case '3':
np += rp;
break;
default:
cerr << "error: unexpected data character '" << ch << "'" << endl;
return 1;
}
for( size_t j = 0; j < num_rounds; ++j )
harden( count, np, table );
modulus = count % 255;
#ifdef DEBUG
cout << "modulus now = " << modulus << endl;
#endif
np += table[ modulus ][ 0 ];
}
cout << np;
}
return 0;
}