-
Notifications
You must be signed in to change notification settings - Fork 0
/
get2FAkey.prg
76 lines (55 loc) · 2.17 KB
/
get2FAkey.prg
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
REQUEST HB_CODEPAGE_UTF8EX
#pragma -w3
procedure Main()
local cSecretKey as character
local cSecretKeyPath as character:="/root/2FA/"
local cSecretKeyFile as character:=hb_FNameMerge(cSecretKeyPath,"hb_2FAsecret_key",".txt")
hb_cdpSelect("UTF8EX")
if (!hb_DirExists(cSecretKeyPath))
hb_DirCreate(cSecretKeyPath)
endif
if (hb_FileExists(cSecretKeyFile))
? "A chave secreta já existe em ",cSecretKeyFile
else
cSecretKey:=Generate2FAKey()
if ((!Empty(cSecretKey)).and.(hb_MemoWrit(cSecretKeyFile,cSecretKey)).and.(hb_FileExists(cSecretKeyFile)))
hb_Run("chmod +600 "+cSecretKeyFile)
? "Chave secreta gerada e armazenada em ",cSecretKeyFile
else
? "Problema na geração do arquivo ",cSecretKeyFile
endif
endif
return
static function Generate2FAKey()
local cCmd as character
local cSecretKey as character
local cTmpSecretKey as character
local cBase32Secret as character
// Gerar 20 bytes aleatórios usando OpenSSL
cTmpSecretKey:="/root/hb_2FA_tmp_secret_key"
hb_run("openssl rand -base64 20 > "+cTmpSecretKey)
// Verifica se o arquivo foi gerado com a chave
if (hb_FileExists(cTmpSecretKey))
cSecretKey:=hb_memoread(cTmpSecretKey)
hb_FileDelete(cTmpSecretKey)
cSecretKey:=strTran(cSecretKey,hb_eol(),"")
// Converter a chave secreta para Base32 usando Python
#pragma __cstream | cCmd:=%s
python3 -c \"import base64; print(base64.b32encode(base64.b64decode('cSecretKey')).decode('utf-8'))\" > cTmpSecretKey
#pragma __endtext
cCmd:=strTran(cCmd,"cSecretKey",cSecretKey)
cCmd:=strTran(cCmd,"cTmpSecretKey",cTmpSecretKey)
// Converter a chave secreta para Base32 usando Python
hb_run(cCmd)
if (hb_FileExists(cTmpSecretKey))
cBase32Secret:=hb_MemoRead(cTmpSecretKey)
hb_FileDelete(cTmpSecretKey)
else
cBase32Secret:=""
endif
else
cBase32Secret:=""
endif
// Remover quebras de linha
cBase32Secret:=strTran(cBase32Secret,hb_eol(),"")
return(cBase32Secret) as character