1 /**
2  * Copyright:       Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3  * Authors:         $(Gabriel Gheorghe)
4  * License:         $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5  * Source:          $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/security/types.d)
6  * Documentation:
7  * Coverage:
8 **/
9 module liberty.security.types;
10 
11 import liberty.security.constants;
12 
13 /**
14  *
15 **/
16 struct Crypted(T) {
17 
18   /**
19    *
20   **/
21   alias type = T;
22 
23   /**
24    *
25   **/
26   alias value this;
27 
28   private {
29     T _value;
30     T _cryptedKey;
31     SecureLevel _secureLevel;
32   }
33 
34   /**
35    *
36   **/
37   this(T value, SecureLevel secure_level = SecureLevel.Level0)   {
38     _secureLevel = secure_level;
39     _value = value;
40     final switch (_secureLevel) with (SecureLevel) {
41       case Level0:
42       case Level1:
43       case Level2:
44       case Level3:
45         generateRandomKey();
46         cryptWithKey();
47         break;
48       case Custom:
49         break;
50     }
51   }
52 
53   /**
54    *
55   **/
56   void setValue(T value)   {
57   }
58 
59   /**
60    *
61   **/
62   T getValue()   const {
63     return _value - _cryptedKey;
64   }
65 
66   private void generateRandomKey()  {
67     import std.random : uniform, Random;
68     auto rnd = Random(73);
69     _cryptedKey = uniform!T(rnd);
70   }
71 
72   private void cryptWithKey()   {
73     _value += _cryptedKey;
74   }
75 
76   private void decryptWithKey()   {
77     _value -= _cryptedKey;
78   }
79 }