rijn 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b05fbe9b7b93479f4b3d616eb912861ffb0b6b4c2e07fe5f08855fa0573aa910
4
+ data.tar.gz: ad55b729ca1f5340b631e4478dcc705963b00106e6a9881e1b30849c83cc8ab8
5
+ SHA512:
6
+ metadata.gz: ece30634d9831a34c2930be796ad886856020e6ffbc558990c745f54e712aa8619690f8488587b40dd92a12cdaf5e751f3cd8d6f863e14bf2622c11fc6fecc75
7
+ data.tar.gz: 05f419640048b8c140b816bc10864f6ca8141f07e7c990ae29363e555b8da6a952ed5f2aa7339261aadf9813b565e562da8a4258f116e6c75a561e8015047d30
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matthew Moritz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/bin/rijn ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+
5
+ require "rijn_cli"
6
+
7
+ Signal.trap("INT") { puts; exit 130 }
8
+ RijnCLI.start(ARGV)
@@ -0,0 +1,116 @@
1
+ require "base64"
2
+ require "openssl"
3
+ require "securerandom"
4
+
5
+ module Rijn
6
+ KEY_LENGTHS = {
7
+ 128 => 16,
8
+ 192 => 24,
9
+ 256 => 32
10
+ }.freeze
11
+
12
+ CIPHERS = {
13
+ 16 => "aes-128-gcm",
14
+ 24 => "aes-192-gcm",
15
+ 32 => "aes-256-gcm"
16
+ }.freeze
17
+
18
+ # Rijn uses a 12-byte nonce for AES-GCM.
19
+ NONCE_LENGTH = 12
20
+
21
+ # Rijn stores a 16-byte authentication tag.
22
+ AUTH_TAG_LENGTH = 16
23
+
24
+ # Generates a cryptographically secure AES-GCM encryption key.
25
+ #
26
+ # @param bits [Integer] key size in bits; must be 128, 192, or 256
27
+ # @return [String] Base64-encoded encryption key
28
+ # @raise [InvalidKeyError] if the key size is unsupported
29
+ def self.generate_key(bits = 256)
30
+ length = KEY_LENGTHS.fetch(bits) do
31
+ raise InvalidKeyError, "Key must be 128, 192, or 256 bits."
32
+ end
33
+
34
+ Base64.strict_encode64(SecureRandom.random_bytes(length))
35
+ end
36
+
37
+ # Encrypts a value using AES-GCM.
38
+ #
39
+ # @param value [String] the plaintext value to encrypt
40
+ # @param key [String] Base64-encoded encryption key (128, 192, or 256 bits)
41
+ # @return [String] Base64-encoded encrypted value
42
+ # @raise [InvalidKeyError] if the key is invalid
43
+ def self.encrypt(value, key)
44
+ key = decode_key(key)
45
+ validate_key!(key)
46
+
47
+ cipher = cipher_for(key)
48
+ cipher.encrypt
49
+
50
+ nonce = SecureRandom.random_bytes(NONCE_LENGTH)
51
+
52
+ cipher.key = key
53
+ cipher.iv = nonce
54
+
55
+ ciphertext = cipher.update(value) + cipher.final
56
+
57
+ tag = cipher.auth_tag
58
+
59
+ Base64.strict_encode64(nonce + ciphertext + tag)
60
+ end
61
+
62
+ # Decrypts a value encrypted with AES-GCM.
63
+ #
64
+ # @param encrypted_value [String] Base64-encoded encrypted value
65
+ # @param key [String] Base64-encoded encryption key (128, 192, or 256 bits)
66
+ # @return [String] the decrypted plaintext value
67
+ # @raise [InvalidKeyError] if the key is invalid
68
+ # @raise [AuthenticationError] if the encrypted value fails authentication
69
+ def self.decrypt(encrypted_value, key)
70
+ key = decode_key(key)
71
+ validate_key!(key)
72
+
73
+ # Rijn stores encrypted values as:
74
+ #
75
+ # nonce (12 bytes) + ciphertext + authentication tag (16 bytes)
76
+ #
77
+ # The nonce is not secret and is required for decryption.
78
+ # The authentication tag is used by AES-GCM to verify the ciphertext.
79
+ decoded = Base64.strict_decode64(encrypted_value)
80
+
81
+ nonce = decoded.byteslice(0, NONCE_LENGTH)
82
+ ciphertext = decoded.byteslice(NONCE_LENGTH...-AUTH_TAG_LENGTH)
83
+ tag = decoded.byteslice(-AUTH_TAG_LENGTH, AUTH_TAG_LENGTH)
84
+
85
+ cipher = cipher_for(key)
86
+ cipher.decrypt
87
+
88
+ cipher.key = key
89
+ cipher.iv = nonce
90
+ cipher.auth_tag = tag
91
+
92
+ begin
93
+ cipher.update(ciphertext) + cipher.final
94
+ rescue OpenSSL::Cipher::CipherError
95
+ raise AuthenticationError, "Unable to decrypt value."
96
+ end
97
+ end
98
+
99
+ private
100
+
101
+ def self.decode_key(key)
102
+ Base64.strict_decode64(key)
103
+ rescue ArgumentError
104
+ raise InvalidKeyError, "Key is not valid Base64."
105
+ end
106
+
107
+ def self.validate_key!(key)
108
+ unless KEY_LENGTHS.value?(key.bytesize)
109
+ raise InvalidKeyError, "Key must be 128, 192, or 256 bits."
110
+ end
111
+ end
112
+
113
+ def self.cipher_for(key)
114
+ OpenSSL::Cipher.new(CIPHERS.fetch(key.bytesize))
115
+ end
116
+ end
@@ -0,0 +1,9 @@
1
+ module Rijn
2
+ # Raised when an encryption key is invalid.
3
+ class InvalidKeyError < StandardError
4
+ end
5
+
6
+ # Raised when an encrypted value cannot be authenticated during decryption.
7
+ class AuthenticationError < StandardError
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Rijn
2
+ # The current version of the Rijn library.
3
+ VERSION = "0.4.0"
4
+ end
data/lib/rijn.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "rijn/version"
2
+ require "rijn/errors"
3
+ require "rijn/crypto"
data/lib/rijn_cli.rb ADDED
@@ -0,0 +1,71 @@
1
+ require "rijn"
2
+ require "pastel"
3
+ require "thor"
4
+ require "tty-prompt"
5
+
6
+ PROMPT = TTY::Prompt.new(interrupt: -> { puts; exit(130) })
7
+ PASTEL = Pastel.new
8
+
9
+ class RijnCLI < Thor
10
+ def self.exit_on_failure?
11
+ true
12
+ end
13
+
14
+ desc "version", "Display the version."
15
+ def version
16
+ write_info(Rijn::VERSION)
17
+ end
18
+
19
+ desc "decrypt", "Decrypt an AES-GCM value."
20
+ option :value, aliases: "-v"
21
+ option :key, aliases: "-k"
22
+ def decrypt
23
+ value = options[:value] || prompt_ask("What's the value?")
24
+ key = options[:key] || prompt_mask("What's the key?")
25
+
26
+ write_info(Rijn.decrypt(value, key))
27
+ rescue Rijn::InvalidKeyError, Rijn::AuthenticationError => e
28
+ write_error(e.message)
29
+ exit 1
30
+ end
31
+
32
+ desc "encrypt", "Encrypt a value using AES-GCM."
33
+ option :value, aliases: "-v"
34
+ option :key, aliases: "-k"
35
+ def encrypt
36
+ value = options[:value] || prompt_ask("What's the value?")
37
+ key = options[:key] || prompt_mask("What's the key?")
38
+
39
+ write_info(Rijn.encrypt(value, key))
40
+ rescue Rijn::InvalidKeyError, Rijn::AuthenticationError => e
41
+ write_error(e.message)
42
+ exit 1
43
+ end
44
+
45
+ desc "keygen", "Generate a new encryption key."
46
+ option :bits, aliases: "-b", type: :numeric, default: 256, desc: "The number of bits for the key (128, 192, or 256)."
47
+ def keygen
48
+ write_info(Rijn.generate_key(options[:bits]))
49
+ rescue Rijn::InvalidKeyError => e
50
+ write_error(e.message)
51
+ exit 1
52
+ end
53
+
54
+ private
55
+
56
+ def prompt_ask(message)
57
+ PROMPT.ask(PASTEL.cyan(message))
58
+ end
59
+
60
+ def prompt_mask(message)
61
+ PROMPT.mask(PASTEL.cyan(message))
62
+ end
63
+
64
+ def write_info(message)
65
+ puts message
66
+ end
67
+
68
+ def write_error(message)
69
+ $stderr.puts PASTEL.red("Error: #{message}")
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rijn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - matthew-moritz
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: base64
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: pastel
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: thor
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: tty-prompt
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ description: A small Ruby library and CLI for AES-GCM encryption, supporting 128-bit,
69
+ 192-bit, and 256-bit keys.
70
+ executables:
71
+ - rijn
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - LICENSE.md
76
+ - bin/rijn
77
+ - lib/rijn.rb
78
+ - lib/rijn/crypto.rb
79
+ - lib/rijn/errors.rb
80
+ - lib/rijn/version.rb
81
+ - lib/rijn_cli.rb
82
+ homepage: https://github.com/matthew-moritz/rijn
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ source_code_uri: https://github.com/matthew-moritz/rijn
87
+ bug_tracker_uri: https://github.com/matthew-moritz/rijn/issues
88
+ rubygems_mfa_required: 'true'
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 4.0.18
104
+ specification_version: 4
105
+ summary: A small CLI for AES-GCM encryption
106
+ test_files: []