-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.fc
70 lines (58 loc) · 2.05 KB
/
4.fc
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
{-
TASK 4 - Caesar Cipher
Implement a Caesar cipher encryption and decryption functions.
The input is a string (https://docs.ton.org/develop/smart-contracts/guidelines/internal-messages#simple-message-with-comment)
where the text is encrypted in a cell (after 32bit 00000000 prefix flag), the algorithm rotates the characters and the last ASCII
character should go to the beginning. Return new text encoded in cell.
-}
() recv_internal() {
}
(builder) encrypt(int shift, slice body) method_id {
builder result = begin_cell();
do {
int c = body~load_uint(8);
if ((c >= 65) & (c <= 90)) {
c = 65 + (c - 65 + shift) % 26;
}
if ((c >= 97) & (c <= 122)) {
c = 97 + (c - 97 + shift) % 26;
}
result~store_uint(c, 8);
} until (body.slice_data_empty?())
return result;
}
builder innerEncode(int shift, cell tree) method_id {
slice body = tree.begin_parse();
builder result = encrypt(shift, body);
ifnot (body.slice_refs_empty?()) { ;; we should remind that -1 is true
builder res = innerEncode(shift, body~load_ref());
result = result.store_ref(res.end_cell());
}
return result;
}
;; testable
(cell) caesar_cipher_encrypt(int shift, cell text) method_id {
builder result = begin_cell();
slice body = text.begin_parse();
int flags = body~load_uint(32);
result~store_uint(flags, 32);
result = store_builder(result, encrypt(shift, body));
ifnot (body.slice_refs_empty?()) { ;; we should remind that -1 is true
result = result.store_ref(innerEncode(shift, body~load_ref()).end_cell());
}
return result.end_cell();
}
;; testable
(cell) caesar_cipher_decrypt(int shift, cell text) method_id {
builder result = begin_cell();
slice body = text.begin_parse();
int flags = body~load_uint(32);
result~store_uint(flags, 32);
result = store_builder(result, encrypt(-1 * shift, body));
ifnot (body.slice_refs_empty?()) { ;; we should remind that -1 is true
result = result.store_ref(innerEncode(-1 * shift, body~load_ref()).end_cell());
}
return result.end_cell();
}
;; ;; 65 -> 67
;; ;; 89 -> 66