-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.fc
55 lines (46 loc) · 1.54 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
{-
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() {
}
(int) shifted(int ch, int shift) inline {
if ((ch >= 97) & (ch <= 122)) {
return (ch - 97 + shift) % 26 + 97;
} else {
if ((ch >= 65) & (ch <= 90)) {
return (ch - 65 + shift) % 26 + 65;
} else {
return ch;
}
}
}
(cell) convert(builder b, slice s, int shift) {
while (~ slice_data_empty?(s)) {
int ch = s~load_uint(8);
b = b.store_uint(shifted(ch, shift), 8);
}
if (~ slice_refs_empty?(s)) {
builder nextb = begin_cell();
slice nexts = s.preload_ref().begin_parse();
b = b.store_ref(convert(nextb, nexts, shift));
}
return b.end_cell();
}
;; testable
(cell) caesar_cipher_encrypt(int shift, cell text) method_id {
slice s = text.begin_parse();
int op = s~load_uint(32);
builder b = begin_cell().store_uint(op, 32);
return convert(b, s, shift);
}
;; testable
(cell) caesar_cipher_decrypt(int shift, cell text) method_id {
slice s = text.begin_parse();
int op = s~load_uint(32);
builder b = begin_cell().store_uint(op, 32);
return convert(b, s, 26 - shift);
}