izokatu 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data.tar.gz.sig +2 -0
  4. data/.gitignore +19 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +42 -0
  7. data/.yardopts +1 -0
  8. data/Gemfile +8 -0
  9. data/Gemfile.lock +86 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +152 -0
  12. data/Rakefile +8 -0
  13. data/bin/console +15 -0
  14. data/bin/setup +8 -0
  15. data/certs/mongalless.pem +26 -0
  16. data/izokatu.gemspec +47 -0
  17. data/lib/izokatu.rb +423 -0
  18. data/lib/izokatu/action_call_options_selector.rb +214 -0
  19. data/lib/izokatu/action_call_selector.rb +132 -0
  20. data/lib/izokatu/callable.rb +13 -0
  21. data/lib/izokatu/ciphers.rb +18 -0
  22. data/lib/izokatu/decrypter.rb +62 -0
  23. data/lib/izokatu/encrypter.rb +67 -0
  24. data/lib/izokatu/exporter.rb +36 -0
  25. data/lib/izokatu/exporter/file_exporter.rb +36 -0
  26. data/lib/izokatu/exporter/function_exporter.rb +16 -0
  27. data/lib/izokatu/exporter/stdout_exporter.rb +18 -0
  28. data/lib/izokatu/helpers.rb +213 -0
  29. data/lib/izokatu/importer/file_importer.rb +47 -0
  30. data/lib/izokatu/importer/function_importer.rb +36 -0
  31. data/lib/izokatu/izokatu_elements_requires.rb +46 -0
  32. data/lib/izokatu/keys_generator.rb +53 -0
  33. data/lib/izokatu/openssl/private_key/auth/ccm/decrypter.rb +67 -0
  34. data/lib/izokatu/openssl/private_key/auth/ccm/encrypter.rb +56 -0
  35. data/lib/izokatu/openssl/private_key/auth/decrypter.rb +79 -0
  36. data/lib/izokatu/openssl/private_key/auth/encrypter.rb +69 -0
  37. data/lib/izokatu/openssl/private_key/default/decrypter.rb +75 -0
  38. data/lib/izokatu/openssl/private_key/default/encrypter.rb +75 -0
  39. data/lib/izokatu/openssl/public_key/ec/decrypter.rb +105 -0
  40. data/lib/izokatu/openssl/public_key/ec/encrypter.rb +106 -0
  41. data/lib/izokatu/openssl/public_key/ec/keys_generator.rb +77 -0
  42. data/lib/izokatu/openssl/public_key/rsa/decrypter.rb +53 -0
  43. data/lib/izokatu/openssl/public_key/rsa/encrypter.rb +55 -0
  44. data/lib/izokatu/openssl/public_key/rsa/keys_generator.rb +64 -0
  45. data/lib/izokatu/rbnacl/decrypter.rb +42 -0
  46. data/lib/izokatu/rbnacl/encrypter.rb +45 -0
  47. data/lib/izokatu/rbnacl/private_key/decrypter.rb +56 -0
  48. data/lib/izokatu/rbnacl/private_key/encrypter.rb +61 -0
  49. data/lib/izokatu/rbnacl/public_key/decrypter.rb +51 -0
  50. data/lib/izokatu/rbnacl/public_key/encrypter.rb +61 -0
  51. data/lib/izokatu/rbnacl/public_key/keys_generator.rb +33 -0
  52. data/lib/izokatu/version.rb +6 -0
  53. metadata +315 -0
  54. metadata.gz.sig +0 -0
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PublicKey
6
+ module RSA
7
+ # OpenSSL public key RSA encrypter
8
+ class Encrypter < Izokatu::Encrypter
9
+ # @return [String] public key string for decryption
10
+ attr_reader :public_key
11
+
12
+ Contract Contracts::HashOf[Symbol, String] => Any
13
+ # Initialize options for OpenSSL RSA encryption
14
+ #
15
+ # @param clear_data (#clear_data)
16
+ # @param public_key (#public_key)
17
+ #
18
+ # @since 0.1.0
19
+ def initialize(clear_data:, public_key:)
20
+ super(clear_data: clear_data)
21
+ initialize_public_key!(public_key)
22
+ end
23
+
24
+ Contract String => OpenSSL::PKey::RSA
25
+ # Initialize RSA public key from public key string
26
+ #
27
+ # @param public_key (#public_key)
28
+ #
29
+ # @return [OpenSSL:PKey::RSA] OpenSSL public key instance
30
+ #
31
+ # @since 0.1.0
32
+ def initialize_public_key!(public_key)
33
+ raise 'ERROR: No public key!' unless public_key
34
+
35
+ @public_key = OpenSSL::PKey::RSA.new(public_key)
36
+ end
37
+
38
+ private
39
+
40
+ Contract None => Contracts::ArrayOf[Contracts::HashOf[Symbol, String]]
41
+ # Encrypting data
42
+ #
43
+ # @return [Array] encrypted data with empty hash in place of params
44
+ #
45
+ # @since 0.1.0
46
+ def encrypt_data!
47
+ raise 'ERROR: No public key!' unless public_key
48
+
49
+ [{ encrypted_data_string: public_key.public_encrypt(clear_data) }, {}]
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PublicKey
6
+ # Namespace for OpenSSL public key RSA classes
7
+ module RSA
8
+ # OpenSSL RSA keys generator
9
+ class KeysGenerator < Izokatu::KeysGenerator
10
+ # @return [Integer] bit number for OpenSSL RSA encryption/decryption
11
+ attr_reader :bit_number
12
+
13
+ # Default options for OpenSSL RSA keys generation
14
+ DEFAULT_OPTIONS = {
15
+ bit_number: 4096
16
+ }.freeze
17
+
18
+ Contract Contracts::HashOf[Symbol, Pos] => Any
19
+ # Initialize option for OpenSSL RSA keys generation
20
+ #
21
+ # @param bit_number (#bit_number)
22
+ #
23
+ # @since 0.1.0
24
+ def initialize(bit_number:)
25
+ super()
26
+ @bit_number = bit_number || DEFAULT_OPTIONS[:bit_number]
27
+ end
28
+
29
+ Contract None => Contracts::HashOf[Or[*KEYS_SYMBOLS], String]
30
+ # Performing generation of OpenSSL RSA private and public keys
31
+ #
32
+ # @return [Hash] OpenSSL RSA public and private keys
33
+ #
34
+ # @since 0.1.0
35
+ def perform
36
+ super.transform_values(&:to_pem)
37
+ end
38
+
39
+ private
40
+
41
+ Contract None => OpenSSL::PKey::RSA
42
+ # Performing generation of OpenSSL RSA private key
43
+ #
44
+ # @return [OpenSSL::PKey::RSA] OpenSSL private key
45
+ #
46
+ # @since 0.1.0
47
+ def generate_private_key
48
+ OpenSSL::PKey::RSA.new(bit_number)
49
+ end
50
+
51
+ Contract OpenSSL::PKey::RSA => OpenSSL::PKey::RSA
52
+ # Performing generation of OpenSSL RSA private key
53
+ #
54
+ # @return [OpenSSL::PKey::RSA] OpenSSL private key
55
+ #
56
+ # @since 0.1.0
57
+ def generate_public_key(private_key)
58
+ private_key.public_key
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Namespace for RbNaCl classes
5
+ module Rbnacl
6
+ # Abstract class for Rbnacl decrypters
7
+ #
8
+ # @abstract Subclasses are containing implementation of {#create_decrypter!}
9
+ class Decrypter < Izokatu::Decrypter
10
+ # @return [String] initialization vector for one-time use
11
+ attr_reader :nonce
12
+
13
+ # Classes of RbNaCl keys
14
+ RBNACL_KEY_CLASSES = [
15
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey,
16
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey
17
+ ].freeze
18
+
19
+ Contract Contracts::HashOf[Symbol, String] => Any
20
+ # Initializing option for decryption
21
+ #
22
+ # @param encrypted_data (#encrypted_data)
23
+ # @param nonce (#nonce)
24
+ #
25
+ # @since 0.1.0
26
+ def initialize(encrypted_data:, nonce:)
27
+ super(encrypted_data: encrypted_data)
28
+ @nonce = nonce
29
+ create_decrypter!
30
+ end
31
+
32
+ # Creating decrypter instance
33
+ #
34
+ # @raise RuntimeError
35
+ #
36
+ # @since 0.1.0
37
+ def create_decrypter!
38
+ raise 'Not implemented!'
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Namespace for RbNaCl classes
5
+ module Rbnacl
6
+ # Abstract class for Rbnacl encrypters
7
+ #
8
+ # @abstract Subclasses are containing implementation of {#create_encrypter!}
9
+ class Encrypter < Izokatu::Encrypter
10
+ # @return [String] initialization vector for one-time use
11
+ attr_reader :nonce
12
+
13
+ Contract Contracts::HashOf[Symbol, String] => Any
14
+ # Initializing option for encryption
15
+ #
16
+ # @param clear_data (#clear_data)
17
+ #
18
+ # @since 0.1.0
19
+ def initialize(clear_data:)
20
+ super
21
+ create_encrypter!
22
+ generate_nonce!
23
+ end
24
+
25
+ # Creating encrypter instance
26
+ #
27
+ # @raise RuntimeError
28
+ #
29
+ # @since 0.1.0
30
+ def create_encrypter!
31
+ raise 'Not implemented!'
32
+ end
33
+
34
+ Contract None => String
35
+ # Generating nonce from encrypter instance
36
+ #
37
+ # @return [String] initialization vector for one-time use
38
+ #
39
+ # @since 0.1.0
40
+ def generate_nonce!
41
+ @nonce = RbNaCl::Random.random_bytes(encrypter.nonce_bytes)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Rbnacl
5
+ # Namespace for RbNaCl private key classes
6
+ module PrivateKey
7
+ # RbNaCl private key decrypter
8
+ class Decrypter < Izokatu::Rbnacl::Decrypter
9
+ # @return [String] key for private key encryption/decryption
10
+ attr_reader :key
11
+ # @return [String] authenticated data
12
+ attr_reader :auth_data
13
+
14
+ # Default options for Izokatu::Rbnacl::Decrypter
15
+ DEFAULT_OPTIONS = {
16
+ auth_data: ''
17
+ }.freeze
18
+
19
+ Contract Contracts::HashOf[Symbol, Or[Or[*RBNACL_KEY_CLASSES], Maybe[String]]] => Any
20
+ # Initializing option for decryption
21
+ #
22
+ # @param encrypted_data (#encrypted_data)
23
+ # @param nonce (#nonce)
24
+ # @param key (#key)
25
+ # @param auth_data (#auth_data)
26
+ #
27
+ # @since 0.1.0
28
+ def initialize(key:, encrypted_data:, nonce:, auth_data:)
29
+ @key = key
30
+ super(encrypted_data: encrypted_data, nonce: nonce)
31
+ @auth_data = auth_data || DEFAULT_OPTIONS[:auth_data]
32
+ end
33
+
34
+ Contract None => RbNaCl::AEAD::XChaCha20Poly1305IETF
35
+ # Creating decrypter instance
36
+ #
37
+ # @return [RbNaCl::AEAD::XChaCha20Poly1305IETF] decrypter instance
38
+ #
39
+ # @since 0.1.0
40
+ def create_decrypter!
41
+ @decrypter = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(key)
42
+ end
43
+
44
+ Contract None => Contracts::HashOf[Symbol, String]
45
+ # Decrypting data
46
+ #
47
+ # @return [Hash] decrypted data
48
+ #
49
+ # @since 0.1.0
50
+ def decrypt_data!
51
+ { decrypted_data_string: decrypter.decrypt(nonce, encrypted_data, auth_data) }
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Rbnacl
5
+ module PrivateKey
6
+ # RbNaCl private key encrypter
7
+ class Encrypter < Izokatu::Rbnacl::Encrypter
8
+ # @return [String] key for private key encryption/decryption
9
+ attr_reader :key
10
+ # @return [String] authenticated data
11
+ attr_reader :auth_data
12
+
13
+ Contract Contracts::HashOf[Symbol, Maybe[String]] => Any
14
+ # Initializing option for encryption
15
+ #
16
+ # @param clear_data (#clear_data)
17
+ # @param auth_data (#auth_data)
18
+ #
19
+ # @since 0.1.0
20
+ def initialize(clear_data:, auth_data:)
21
+ generate_key!
22
+ super(clear_data: clear_data)
23
+ @auth_data = auth_data
24
+ end
25
+
26
+ Contract None => String
27
+ # Generating key
28
+ #
29
+ # @since 0.1.0
30
+ def generate_key!
31
+ @key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
32
+ end
33
+
34
+ Contract None => RbNaCl::AEAD::XChaCha20Poly1305IETF
35
+ # Generating encrypter instance from key
36
+ #
37
+ # @since 0.1.0
38
+ def create_encrypter!
39
+ @encrypter = RbNaCl::AEAD::XChaCha20Poly1305IETF.new(key)
40
+ end
41
+
42
+ Contract None => Contracts::ArrayOf[Contracts::HashOf[Symbol, Maybe[String]]]
43
+ # Encrypting data
44
+ #
45
+ # @return (Array) encrypted data with decrypter params
46
+ #
47
+ # @since 0.1.0
48
+ def encrypt_data!
49
+ [
50
+ { encrypted_data_string: encrypter.encrypt(nonce, clear_data, auth_data) },
51
+ {
52
+ auth_data: auth_data,
53
+ key: key,
54
+ nonce: nonce
55
+ }
56
+ ]
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Rbnacl
5
+ # Namespace for RbNaCl public key classes
6
+ module PublicKey
7
+ # RbNaCl public key decrypter
8
+ class Decrypter < Izokatu::Rbnacl::Decrypter
9
+ # @return [RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey] public key
10
+ attr_reader :public_key
11
+ # @return [RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey] private key
12
+ attr_reader :private_key
13
+
14
+ Contract Contracts::HashOf[Symbol, Or[Or[*RBNACL_KEY_CLASSES], String]] => Any
15
+ # Initializing option for decryption
16
+ #
17
+ # @param encrypted_data (#encrypted_data)
18
+ # @param nonce (#nonce)
19
+ # @param public_key (#public_key)
20
+ # @param private_key (#private_key)
21
+ #
22
+ # @since 0.1.0
23
+ def initialize(public_key:, private_key:, encrypted_data:, nonce:)
24
+ @public_key = public_key
25
+ @private_key = private_key
26
+ super(encrypted_data: encrypted_data, nonce: nonce)
27
+ end
28
+
29
+ Contract None => RbNaCl::Boxes::Curve25519XSalsa20Poly1305
30
+ # Initializing decrypter instance
31
+ #
32
+ # @return [RbNaCl::Boxes::Curve25519XSalsa20Poly1305] decrypter instance
33
+ #
34
+ # @since 0.1.0
35
+ def create_decrypter!
36
+ @decrypter = RbNaCl::Box.new(public_key, private_key)
37
+ end
38
+
39
+ Contract None => Contracts::HashOf[Symbol, String]
40
+ # Decrypting data
41
+ #
42
+ # @return [Hash] decrypted data
43
+ #
44
+ # @since 0.1.0
45
+ def decrypt_data!
46
+ { decrypted_data_string: decrypter.decrypt(nonce, encrypted_data) }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Rbnacl
5
+ module PublicKey
6
+ # RbNaCl public key encrypter
7
+ class Encrypter < Izokatu::Rbnacl::Encrypter
8
+ # @return [RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey] public key
9
+ attr_reader :public_key
10
+ # @return [RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey] private key
11
+ attr_reader :private_key
12
+
13
+ # RbNaCl public and private key classes
14
+ RBNACL_KEY_CLASSES = [
15
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey,
16
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey
17
+ ].freeze
18
+
19
+ Contract Contracts::HashOf[Symbol, Or[String, Or[*RBNACL_KEY_CLASSES]]] => Any
20
+ # Initializing option for encryption
21
+ #
22
+ # @param clear_data (#clear_data)
23
+ # @param public_key (#public_key)
24
+ # @param private_key (#private_key)
25
+ #
26
+ # @since 0.1.0
27
+ def initialize(public_key:, private_key:, clear_data:)
28
+ @public_key = public_key
29
+ @private_key = private_key
30
+ super(clear_data: clear_data)
31
+ end
32
+
33
+ Contract None => RbNaCl::Boxes::Curve25519XSalsa20Poly1305
34
+ # Initializing option for encryption
35
+ #
36
+ # @return [RbNaCl::Boxes::Curve25519XSalsa20Poly1305] encrypter instance
37
+ #
38
+ # @since 0.1.0
39
+ def create_encrypter!
40
+ raise 'ERROR: No public key!' unless public_key
41
+ raise 'ERROR: No private key!' unless private_key
42
+
43
+ @encrypter = RbNaCl::Box.new(public_key, private_key)
44
+ end
45
+
46
+ Contract None => Contracts::ArrayOf[Contracts::HashOf[Symbol, String]]
47
+ # Encrypting data
48
+ #
49
+ # @return [Array] encrypted data with decrypter params
50
+ #
51
+ # @since 0.1.0
52
+ def encrypt_data!
53
+ [
54
+ { encrypted_data_string: encrypter.encrypt(nonce, clear_data) },
55
+ { nonce: nonce }
56
+ ]
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Rbnacl
5
+ module PublicKey
6
+ # RbNaCl keys generator
7
+ class KeysGenerator < Izokatu::KeysGenerator
8
+ private
9
+
10
+ Contract None => RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey
11
+ # Performing generation of RbNaCl private key
12
+ #
13
+ # @return [RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey] RbNaCl private key
14
+ #
15
+ # @since 0.1.0
16
+ def generate_private_key
17
+ RbNaCl::PrivateKey.generate
18
+ end
19
+
20
+ Contract RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey =>
21
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey
22
+ # Performing generation of RbNaCl public key
23
+ #
24
+ # @return [RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey] RbNaCl public key
25
+ #
26
+ # @since 0.1.0
27
+ def generate_public_key(private_key)
28
+ private_key.public_key
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end