why2/legacy/
options.rs

1/*
2This is part of WHY2
3Copyright (C) 2022-2026 Václav Šmejkal
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#![allow(deprecated)]
20
21use std::sync::{ LazyLock, RwLock };
22
23//ENUMS
24//THESE ARE LEGACY VERSIONS FOR GENERATING tkch, SO YOU CAN DECRYPT OLD TEXT
25#[derive(Clone, PartialEq)]
26pub enum Version
27{
28    V1, //FIRST VERSION. Replaced on May 28th 17:45:26 2022 UTC in commit 0d64f4fa7c37f0b57914db902258e279a71c7f9a.
29    V2, //SECOND VERSION. Replaced on July 11th 17:12:41 2022 UTC in commit 0f01cde0f1e1a9210f4eef7b949e6d247072d3a6.
30    V3, //THIRD VERSION. Replaced on Nov 17 19:55:13 2024 UTC in commit f917140ae54e4f5e601a089fbbea33817233e534.
31    V4, //LATEST VERSION, MOST SECURE (how unexpected)
32}
33
34//STRUCTS
35#[derive(Clone)]
36pub struct Options
37{
38    pub key_length: usize,                         //LENGTH OF SYMMETRIC KEY
39    pub version: Version,                          //VERSION OF tkch
40    pub padding: usize,                            //HOW MANY PADDING CHARS TO ADD
41    pub encryption_operation: fn(i64, i64) -> i64, //ENCRYPTION OPERATION CLOSURE
42}
43
44pub struct EncryptedData
45{
46    pub output: Vec<i64>, //ENCRYPTED TEXT
47    pub key: String,      //KEY USED FOR ENCRYPTION
48}
49
50pub struct DecryptedData
51{
52    pub output: String, //DECRYPTED DATA
53    pub key: String,    //KEY USED FOR ENCRYPTION
54}
55
56//IMPLEMENTATIONS
57impl Default for Options
58{
59    fn default() -> Self
60    {
61        Self
62        {
63            key_length: 50,
64            version: Version::V4,
65            padding: 64,
66            encryption_operation: |a, b| a - b,
67        }
68    }
69}
70
71//SETTINGS
72static CORE_SETTINGS: LazyLock<RwLock<Options>> = LazyLock::new(||
73{
74    RwLock::new(Options::default())
75});
76
77//FUNCTIONS
78//CORE SETTINGS
79pub fn set_core_options(options: Options) //OVERWRITE DEFAULT SETTINGS
80{
81    let mut settings = CORE_SETTINGS.write().unwrap();
82    *settings = options;
83}
84
85pub fn get_core_options() -> Options //RETURN SETTINGS
86{
87    let options = CORE_SETTINGS.read().unwrap();
88    options.clone()
89}