-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.fc
60 lines (50 loc) · 1.83 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
{-
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.
-}
forall X -> (tuple, ()) push_back (tuple tail, X head) asm "CONS";
forall X -> (tuple, (X)) pop_back (tuple t) asm "UNCONS";
() recv_internal() {
}
(cell) caesar(int shift, cell text) {
tuple stack = null();
stack~push_back(text.begin_parse().skip_bits(32));
builder result = begin_cell().store_uint(0, 32);
tuple result_stack = null();
do {
slice next_slice = stack~pop_back();
repeat (next_slice.slice_bits() / 8) {
int symbol_code = next_slice~load_uint(8);
if ((symbol_code >= 65) & (symbol_code <= 90)) {
result = result.store_uint(65 + ((symbol_code - 65 + shift) % 26), 8);
} else {
if ((symbol_code >= 97) & (symbol_code <= 122)) {
result = result.store_uint(97 + ((symbol_code - 97 + shift) % 26), 8);
} else {
result = result.store_uint(symbol_code, 8);
}
}
}
if (next_slice.slice_refs()) {
stack~push_back(next_slice~load_ref().begin_parse());
result_stack~push_back(result);
result = begin_cell();
}
} until (stack.null?())
while (~ result_stack.null?()) {
builder part = result_stack~pop_back();
result = part.store_ref(result.end_cell());
}
return result.end_cell();
}
;; testable
(cell) caesar_cipher_encrypt(int shift, cell text) method_id {
return caesar(shift, text);
}
;; testable
(cell) caesar_cipher_decrypt(int shift, cell text) method_id {
return caesar(- shift, text);
}