-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafeutils.nim
57 lines (42 loc) · 1.34 KB
/
safeutils.nim
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
import nimAES
import strutils
import std/sha1
var aes = initAES()
const fillCharacter* = 0.char
proc growKey(key: var string) =
for i in 0..<32:
if key.len mod 32 == 0:
break
key.add(key[i])
proc growData(data: var string) =
while data.len mod 16 != 0:
data.add(fillCharacter)
proc encryptText*(key, data: string): string =
var
key = key
data = data
if find(data, fillCharacter) != -1:
raise newException(Exception, "data to encrypt can't include fillCharacter '$1' " % $fillCharacter)
growKey(key)
growData(data)
if aes.setEncodeKey(key):
for i in countup(0, data.len - 1, 16):
result.add(aes.encryptECB(data[i..i+15]))
else:
raise newException(Exception, "aes.SetEncodeKey failed")
assert result.len mod 16 == 0
return result
proc decryptText*(key, data: string): string =
var
key = key
data = data
assert data.len mod 16 == 0
growKey(key)
if aes.setDecodeKey(key):
for i in countup(0, data.len - 1, 16):
result.add(aes.decryptECB(data[i..i+15]))
else:
raise newException(Exception, "aes.setDecodeKey failed")
return result.strip(chars = {fillCharacter})
proc hashsum*(data: string): string =
return $secureHash($secureHash(data))