-
Notifications
You must be signed in to change notification settings - Fork 12
/
sancrypt.red
74 lines (67 loc) · 1.19 KB
/
sancrypt.red
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
Red[
Title: "SANSCrypt - Simple And Naive Symmetric Crypto"
Author: "Boleslav Březovský"
Purpose: "Placeholder symmetric cryptography until Red gets real thing"
]
context [
binc: func [
"Binary increase"
value [binary!]
][
len: length? value
value/:len: value/:len + 1
repeat i len [
i: len - i + 1
either zero? value/:i [
value/(i - 1): value/(i - 1) + 1
][
break
]
]
value
]
make-nonce: func [
"Return random binary. Default is 256 bits"
/size
length "Size in bits"
][
length: any [length 256]
collect/into [
loop length / 8 [keep (random/secure 256) - 1]
] copy #{}
]
crypt: func [
value [string! binary!]
password [string!]
nonce [binary!]
][
data: copy #{}
until [
block: copy/part value 32
value: skip value 32
key: checksum rejoin [#{} password form nonce] 'sha256
block: key xor to binary! block
append data block
binc nonce
tail? value
]
data
]
set 'encrypt func [
value [string! binary!]
password [string!]
][
nonce: make-nonce
orig: copy nonce
value: crypt value password nonce
reduce [orig value]
]
set 'decrypt func [
value [string! binary!]
password [string!]
nonce [binary!]
][
crypt value password nonce
]
; -- end of context
]