-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.h
86 lines (73 loc) · 2.29 KB
/
auth.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
/*
Copyright (c) 2023 William He (https://github.com/fwilliamhe/)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CF_AUTH
#define CF_AUTH
#include <iostream>
#include <cstring>
#include <random>
#include "md5.h"
std::mt19937 rnd(time(nullptr));
int rand(int l,int r) {
return rnd() % (r - l + 1) + l;
}
std::string ito36text(long long x) {
std::string rt="";
while (x){
long long c = x % 36;
if (c < 10) rt += (char)(c + '0');
else rt += (char)(c - 10 + 'a');
x /= 36;
}
return rt;
}
std::string randomNumber() {
return ito36text(abs(rnd())).substr(2);
}
std::string randomToken() {
std::string rt = "";
while (rt.length() < 18) {
rt += randomNumber();
}
return rt.substr(0, 18);
}
namespace codeforces {
/*
Get tta for codeforces
Notes : cookie is cookies['39ce7']
Upd time : Nov 1 2023 19:52:51 (PDT)
*/
int get_tta(std::string cookie) {
int tta = 0; const int P = 1009;
for (int i = 0; i < cookie.length(); ++i) {
tta = (tta + (i + 1) % P * (i + 2) % P * cookie[i] % P) % P;
if (i % 3 == 0) (tta += 1) %= P;
if (i % 2 == 0) (tta *= 2) %= P;
if (i > 0) (tta -= (cookie[i / 2] / 2) % P * (tta % 5) % P - P ) %= P;
}
return tta;
}
/* Generate bfaa
Random string is also valid. Users can stored different sessions with MD5.
*/
std::string generate_bfaa() {
std::string random_string = randomToken();
return hash::MD5(random_string).hexdigest();
// return hash::MD5( <UserSession> ).hexdigest();
}
/* Generate ftaa */
std::string generate_ftaa() {
return randomToken();
}
}
#endif