epithet 0.1.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 +7 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE +21 -0
- data/README.md +70 -0
- data/SECURITY.md +41 -0
- data/examples/basic.rb +16 -0
- data/lib/epithet/version.rb +4 -0
- data/lib/epithet.rb +298 -0
- metadata +98 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c8d4c22a2ee0f83df400b54761a8e5c0a03df04390c6c9cdbb486a8c907f1812
|
|
4
|
+
data.tar.gz: a8507f28f992f524284262d498b107cfb566f9fefb66c5d97e2f29322d7a9fe4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: bc64b08e053da076bab07709bed61f1ff0c41ebcdb59a3963f63b7846b897efa2b8a2de43330b8bb0a3d7b473d377dc676da2ef4331ebcd52d18749809b8b1c3
|
|
7
|
+
data.tar.gz: 8495ae0d1ed045619ed752ef55cc240ff5180b6da68bf27f2cc858e6ec1a190eb93e10c5dc1e23665a363ee42448dc1df6d23f3522ab03813f6d2f48da322b44
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Josh Goodall
|
|
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/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Epithet
|
|
2
|
+
|
|
3
|
+
Epithet generates stable, compact, purposefully prefixed Base58 identifiers from 64-bit integers for reversible obfuscation and tamper detection.
|
|
4
|
+
|
|
5
|
+
* https://github.com/inopinatus/epithet
|
|
6
|
+
* https://inopinatus.github.io/epithet/
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add to your Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'epithet'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Or install directly:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
gem install epithet
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
require 'epithet'
|
|
26
|
+
|
|
27
|
+
def epithet_initialize
|
|
28
|
+
Epithet.configure(
|
|
29
|
+
passphrase: ENV.fetch('EPITHET_PASSPHRASE') { 'example only' },
|
|
30
|
+
salt: 'v1'
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
epithet_initialize
|
|
35
|
+
user_epithet = Epithet.new('user')
|
|
36
|
+
|
|
37
|
+
id = 42
|
|
38
|
+
param = user_epithet.encode(id)
|
|
39
|
+
# => "user_VsuNnfEYQJJTJYE3n28jaY"
|
|
40
|
+
|
|
41
|
+
user_epithet.decode(param)
|
|
42
|
+
# => 42
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Configuration at initialisation is recommended, because deriving key material from the passphrase uses scrypt, and is consequently expensive. The `salt:` is optional; it's included when deriving the subkey material for obfuscating and tamper resistance, and may be useful for additional context discrimination or during secrets rotation.
|
|
46
|
+
|
|
47
|
+
Refer to the Epithet rdoc for the full set of configuration options.
|
|
48
|
+
|
|
49
|
+
Note that `decode` returns `nil` when authentication fails and raises ArgumentError on invalid formats.
|
|
50
|
+
|
|
51
|
+
## Development
|
|
52
|
+
|
|
53
|
+
Install dependencies and run tests:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
bundle install
|
|
57
|
+
rake test
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Contributing
|
|
61
|
+
|
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/inopinatus/epithet.
|
|
63
|
+
|
|
64
|
+
## Security considerations
|
|
65
|
+
|
|
66
|
+
See [`SECURITY.md`](SECURITY.md).
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/SECURITY.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Epithet security
|
|
2
|
+
|
|
3
|
+
## Cryptographic considerations
|
|
4
|
+
|
|
5
|
+
The primary construction is `AES-256-ECB(id(8B) + HMAC-SHA256(id)[0,7])` with the result
|
|
6
|
+
base58 encoded for transmission and a contextual prefix prepended. Subkeys for AES and
|
|
7
|
+
HMAC are by default derived with HKDF using an internal key generator that takes IKM from
|
|
8
|
+
a passphrase via scrypt, salting generated keys by prefix and purpose.
|
|
9
|
+
|
|
10
|
+
This library is intended for high-performance obfuscation of integer sequences, deflection
|
|
11
|
+
of casual tampering, and conversion to a compact, stable wire parameter format. Although
|
|
12
|
+
it uses standard cryptographic primitives to do so, the design trade-off of the compact
|
|
13
|
+
format means it is not intended to defeat nation-state security services, talented
|
|
14
|
+
cryptographers, or even a well-resourced enterprise.
|
|
15
|
+
|
|
16
|
+
The identifiers produced are intentionally deterministic i.e. replayable and reusable. For
|
|
17
|
+
privacy, confidentiality, and authentication purposes they should therefore be considered
|
|
18
|
+
equivalent to the plaintext integer they represent, and those purposes must still be addressed
|
|
19
|
+
in the usual manner.
|
|
20
|
+
|
|
21
|
+
The tamper detection is necessarily probabilistic, because the MAC is truncated.
|
|
22
|
+
|
|
23
|
+
If configuring alternative cipher and digest algorithms, note that only 128-bit block
|
|
24
|
+
ciphers that function without IV/nonce requirements are accepted. Streaming ciphers
|
|
25
|
+
(e.g. chacha20) or block ciphers in streaming modes (e.g. aes-256-ctr) must not be used;
|
|
26
|
+
no nonce/IV value is included in construction, making them trivially vulnerable to
|
|
27
|
+
known-plaintext attacks. These, CBC/OCB, and other IV/nonce modes may also be rejected
|
|
28
|
+
by Epithet's guardrails.
|
|
29
|
+
|
|
30
|
+
A weak, guessable, or disclosed passphrase will compromise the obfuscation and
|
|
31
|
+
tamper-detection properties.
|
|
32
|
+
|
|
33
|
+
Use Epithet at your own risk.
|
|
34
|
+
|
|
35
|
+
## Vulnerabilities
|
|
36
|
+
|
|
37
|
+
If you think you've found a vulnerability in Epithet that compromises its design or behaviour, please
|
|
38
|
+
[report it via a private advisory](https://github.com/inopinatus/epithet/security/advisories/new).
|
|
39
|
+
Do not open a public issue or pull request.
|
|
40
|
+
|
|
41
|
+
|
data/examples/basic.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'epithet'
|
|
2
|
+
|
|
3
|
+
def epithet_initialize
|
|
4
|
+
Epithet.configure(
|
|
5
|
+
passphrase: ENV.fetch('EPITHET_PASSPHRASE') { 'example only' },
|
|
6
|
+
salt: 'v1'
|
|
7
|
+
)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
epithet_initialize
|
|
11
|
+
user_epithet = Epithet.new('user')
|
|
12
|
+
|
|
13
|
+
id = Integer(ARGV.shift || 42)
|
|
14
|
+
param = user_epithet.encode(id) #=> "user_VsuNnfEYQJJTJYE3n28jaY"
|
|
15
|
+
|
|
16
|
+
puts "User(#{user_epithet.decode(param)}) => #{param}"
|
data/lib/epithet.rb
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'epithet/version'
|
|
4
|
+
require 'openssl'
|
|
5
|
+
|
|
6
|
+
#
|
|
7
|
+
# Epithet, a tool for external identifiers.
|
|
8
|
+
#
|
|
9
|
+
# Given a 64-bit value such as a database sequence ID, and a context-specific
|
|
10
|
+
# prefix (typically a model or table name), produces a replayable string parameter of
|
|
11
|
+
# consistent length, with modest obfuscation and authentication properties.
|
|
12
|
+
#
|
|
13
|
+
# Pseudo-AEAD is via `AES-256-ECB(id(8B) + HMAC-SHA256(id)[0,7])` with the result
|
|
14
|
+
# base58 encoded for transmission and the contextual prefix prepended.
|
|
15
|
+
#
|
|
16
|
+
# Subkeys for AES and HMAC are by default derived with HKDF using an internal key
|
|
17
|
+
# generator that takes IKM from a passphrase via scrypt. An alternative key generator
|
|
18
|
+
# may be injected via Config objects. Subkeys are salted by prefix and an optional
|
|
19
|
+
# additional salt, which may be useful for purpose separation or rotation.
|
|
20
|
+
#
|
|
21
|
+
# Example usage:
|
|
22
|
+
#
|
|
23
|
+
# # in setup-environment.sh
|
|
24
|
+
# EPITHET_PASSPHRASE='example only'
|
|
25
|
+
#
|
|
26
|
+
# # ... later, in Ruby ...
|
|
27
|
+
# Epithet.configure(passphrase: ENV.fetch('EPITHET_PASSPHRASE'))
|
|
28
|
+
# user_epithet = Epithet.new('user')
|
|
29
|
+
# user_epithet.encode(1) #=> "user_DAG6Joc5JmgygTBuEo8a9K"
|
|
30
|
+
#
|
|
31
|
+
class Epithet
|
|
32
|
+
# Create an encoder/decoder.
|
|
33
|
+
#
|
|
34
|
+
# Setup could be moderately expensive due to key derivation; you are recommended to cache
|
|
35
|
+
# and reuse instances with equal parameters (e.g. setup once per model)
|
|
36
|
+
#
|
|
37
|
+
# * `prefix` is stringified, and may be nil, producing an empty prefix.
|
|
38
|
+
# The prefix is included in the salt for key generation.
|
|
39
|
+
#
|
|
40
|
+
# * `config` is optional and intended for cases where you needed finer control than global defaults.
|
|
41
|
+
#
|
|
42
|
+
# The simplest typical invocation is `Epithet.new('prefix')`.
|
|
43
|
+
#
|
|
44
|
+
def initialize(prefix, config: Epithet.defaults)
|
|
45
|
+
prefix = String(prefix)
|
|
46
|
+
key_salt = [prefix.bytesize, prefix, config.salt.bytesize, config.salt].pack("Q>Z*Q>Z*")
|
|
47
|
+
@block58 = Block58.new(16)
|
|
48
|
+
@prefix_s = "#{prefix}#{config.separator}"
|
|
49
|
+
|
|
50
|
+
cipher_key_len = OpenSSL::Cipher.new(config.cipher).key_len
|
|
51
|
+
digest_key_len = OpenSSL::Digest.new(config.digest).block_length
|
|
52
|
+
cipher_key = config.keygen.generate("epithet:ecb", key_salt, cipher_key_len)
|
|
53
|
+
digest_key = config.keygen.generate("epithet:mac", key_salt, digest_key_len)
|
|
54
|
+
|
|
55
|
+
@encryptor = OpenSSL::Cipher.new(config.cipher).encrypt.tap { |c| c.key = cipher_key; c.padding = 0 }
|
|
56
|
+
@decryptor = OpenSSL::Cipher.new(config.cipher).decrypt.tap { |c| c.key = cipher_key; c.padding = 0 }
|
|
57
|
+
@hmac = OpenSSL::HMAC.new(digest_key, config.digest)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Encode a 64-bit unsigned Integer to a prefixed Base58 string.
|
|
61
|
+
# Raises ArgumentError on invalid values.
|
|
62
|
+
def encode(id)
|
|
63
|
+
raise ArgumentError, "not a 64-bit unsigned integer" unless Integer === id && id.size == 8 && id >= 0
|
|
64
|
+
|
|
65
|
+
e = @encryptor.dup
|
|
66
|
+
h = @hmac.dup
|
|
67
|
+
pt = [id].pack('Q>')
|
|
68
|
+
m = h.update(pt).digest
|
|
69
|
+
block = e.update([pt, m].pack('a8a8')) + e.final
|
|
70
|
+
ct = block.unpack('Q>2').then { (_1 << 64) + _2 }
|
|
71
|
+
|
|
72
|
+
@prefix_s + @block58.i2s(ct)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Decode a prefixed or raw Base58 string to an Integer.
|
|
76
|
+
#
|
|
77
|
+
# Returns the Integer on success, nil if authentication fails.
|
|
78
|
+
# Raises ArgumentError on invalid formats.
|
|
79
|
+
def decode(s)
|
|
80
|
+
s = s.delete_prefix(@prefix_s)
|
|
81
|
+
raise ArgumentError, "unexpected format" unless @block58.valid?(s)
|
|
82
|
+
|
|
83
|
+
d = @decryptor.dup
|
|
84
|
+
h = @hmac.dup
|
|
85
|
+
ct = @block58.s2i(s)
|
|
86
|
+
block = d.update([ct >> 64, ct].pack('Q>2')) + d.final
|
|
87
|
+
pt, m = block.unpack('a8a8')
|
|
88
|
+
id = pt.unpack1('Q>')
|
|
89
|
+
|
|
90
|
+
cteq([h.update(pt).digest].pack('a8'), m) ? id : nil
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Constant time octet-string comparison.
|
|
94
|
+
def cteq(a, b) # :nodoc:
|
|
95
|
+
return false unless a.bytesize == b.bytesize
|
|
96
|
+
OpenSSL.fixed_length_secure_compare(a, b)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
class << self
|
|
100
|
+
# Configure the library.
|
|
101
|
+
#
|
|
102
|
+
# #### Examples
|
|
103
|
+
#
|
|
104
|
+
# # As it might appear in an initializer
|
|
105
|
+
# Epithet.configure(passphrase: ENV.fetch('EPITHET_PASSPHRASE'))
|
|
106
|
+
#
|
|
107
|
+
# # Retaining already-configured passphrase but updating salt,
|
|
108
|
+
# # and using a custom separator.
|
|
109
|
+
# Epithet.configure(
|
|
110
|
+
# keygen: Epithet.defaults.keygen,
|
|
111
|
+
# salt: 'rotation-19',
|
|
112
|
+
# separator: '-'
|
|
113
|
+
# )
|
|
114
|
+
#
|
|
115
|
+
# #### Options
|
|
116
|
+
#
|
|
117
|
+
# * `:passphrase` - Install new key generator with scrypt-derived key material
|
|
118
|
+
# * `:scrypt` - Params for scrypt; omit to use `Keygen::DEFAULT_SCRYPT_PARAMS`
|
|
119
|
+
# * `:keygen` - Install an existing key generator
|
|
120
|
+
# * `:cipher` - Must be a 128-bit block cipher in ECB mode or equivalent; omit for standard `aes-256-ecb`
|
|
121
|
+
# * `:digest` - Must be >= 64 bits; omit for standard `sha256`
|
|
122
|
+
# * `:separator` - Inserted as string between the prefix and the generated param. may be nil; omit for underscore.
|
|
123
|
+
# * `:salt` - If supplied, stringified form is included in subkey derivation
|
|
124
|
+
#
|
|
125
|
+
# At minimum, one of `passphrase:` or `keygen:` is required.
|
|
126
|
+
# Configuration via `passphrase` is recommended.
|
|
127
|
+
#
|
|
128
|
+
# If passing an existing key generator, the object must respond to `generate(info, salt, length)`
|
|
129
|
+
# and return a byte string suitable for use with OpenSSL cryptographic primitives.
|
|
130
|
+
#
|
|
131
|
+
# See [`SECURITY.md`](SECURITY.md) for discussion of ciphers & digests.
|
|
132
|
+
#
|
|
133
|
+
# #### Multiple configurations
|
|
134
|
+
#
|
|
135
|
+
# You can produce & store configurations, thus:
|
|
136
|
+
#
|
|
137
|
+
# cfg = Epithet::Config.new(
|
|
138
|
+
# keygen: my_key_gen,
|
|
139
|
+
# cipher: 'stronk-512-jcb',
|
|
140
|
+
# digest: 'md7'
|
|
141
|
+
# )
|
|
142
|
+
#
|
|
143
|
+
# and either install this as default with
|
|
144
|
+
#
|
|
145
|
+
# Epithet.configure(cfg)
|
|
146
|
+
#
|
|
147
|
+
# or pass it to `Epithet::new` as
|
|
148
|
+
#
|
|
149
|
+
# acct_epithet = Epithet.new('acct', config: cfg)
|
|
150
|
+
#
|
|
151
|
+
def configure(opts) = @defaults = Config === opts ? opts : Config.new(opts)
|
|
152
|
+
def defaults() = @defaults || raise(RuntimeError, "no Epithet defaults configured")
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Class for passing around preset configs. See Epithet::configure for options.
|
|
156
|
+
class Config
|
|
157
|
+
attr_reader :keygen, :salt, :separator, :cipher, :digest # :nodoc:
|
|
158
|
+
def initialize(opts = {})
|
|
159
|
+
opts = opts.dup
|
|
160
|
+
@separator = String(opts.delete(:separator) { '_' })
|
|
161
|
+
@salt = String(opts.delete(:salt))
|
|
162
|
+
@cipher = opts.delete(:cipher) || 'aes-256-ecb'
|
|
163
|
+
@digest = opts.delete(:digest) || 'sha256'
|
|
164
|
+
|
|
165
|
+
cipher = OpenSSL::Cipher.new(@cipher)
|
|
166
|
+
raise ArgumentError, "#{@cipher} not a 128-bit block cipher" if cipher.block_size != 16
|
|
167
|
+
raise ArgumentError, "#{@cipher} requires an IV/nonce" if cipher.iv_len != 0
|
|
168
|
+
raise ArgumentError, "#{@digest} produces < 64-bit digest" if OpenSSL::Digest.new(@digest).digest_length < 8
|
|
169
|
+
|
|
170
|
+
@keygen = opts.delete(:keygen) || Keygen.new(
|
|
171
|
+
passphrase: opts.delete(:passphrase),
|
|
172
|
+
digest: @digest,
|
|
173
|
+
scrypt: opts.delete(:scrypt) || Keygen::DEFAULT_SCRYPT_PARAMS)
|
|
174
|
+
raise ArgumentError, "unused option(s) #{opts.keys}" unless opts.empty?
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Key derivation helper
|
|
179
|
+
class Keygen
|
|
180
|
+
# Default parameters for scrypt.
|
|
181
|
+
#
|
|
182
|
+
# ```ruby
|
|
183
|
+
# DEFAULT_SCRYPT_PARAMS = {
|
|
184
|
+
# salt: 'epithet-default',
|
|
185
|
+
# N: 1<<17,
|
|
186
|
+
# r: 8,
|
|
187
|
+
# p: 1,
|
|
188
|
+
# length: 32
|
|
189
|
+
# }.freeze
|
|
190
|
+
# ```
|
|
191
|
+
DEFAULT_SCRYPT_PARAMS = {
|
|
192
|
+
salt: 'epithet-default',
|
|
193
|
+
N: 1<<17,
|
|
194
|
+
r: 8,
|
|
195
|
+
p: 1,
|
|
196
|
+
length: 32
|
|
197
|
+
}.freeze
|
|
198
|
+
|
|
199
|
+
# Create a new key generator from either high-entropy key material, or a supplied passphrase.
|
|
200
|
+
def initialize(ikm: nil, passphrase: nil, digest: "sha256", scrypt: DEFAULT_SCRYPT_PARAMS)
|
|
201
|
+
if (passphrase.nil? && ikm.nil?) || (!passphrase.nil? && !ikm.nil?)
|
|
202
|
+
raise ArgumentError, "keygen requires either ikm or passphrase"
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
@ikm = ikm || OpenSSL::KDF.scrypt(passphrase, **scrypt)
|
|
206
|
+
@digest = digest
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def inspect
|
|
210
|
+
"#<#{self.class}:#{'%#016x' % (object_id << 1)} digest=#{@digest}>"
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Derive a key via HKDF.
|
|
214
|
+
def generate(info, salt, length)
|
|
215
|
+
OpenSSL::KDF.hkdf(@ikm, hash: @digest, info: info, salt: salt, length: length)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Fixed-length Base58 codec for a fixed-size block.
|
|
220
|
+
class Block58
|
|
221
|
+
# `= "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"`
|
|
222
|
+
Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
|
223
|
+
|
|
224
|
+
# Create a codec for a block size in bytes.
|
|
225
|
+
def initialize(block_size, alphabet: Alphabet)
|
|
226
|
+
@alphabet = alphabet.b.freeze
|
|
227
|
+
raise ArgumentError, "invalid alphabet length" unless @alphabet.bytesize == 58
|
|
228
|
+
@size = ((block_size * 8) / Math.log2(58)).ceil(0)
|
|
229
|
+
@charsel = @alphabet.gsub(/[\^\-\\]/, '\\\\\&').freeze
|
|
230
|
+
@blank = @alphabet[0] * @size
|
|
231
|
+
@lut = @alphabet.each_byte.with_index.with_object("\0" * 256) { |(val, idx), lut| lut.setbyte(val, idx) }.freeze
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def inspect
|
|
235
|
+
"#<#{self.class}:#{'%#016x' % (object_id << 1)} size=#{@size} alphabet=#{@alphabet}>"
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Return true if the string has the right size and alphabet.
|
|
239
|
+
def valid?(s)
|
|
240
|
+
String === s && s.bytesize == @size && s.count(@charsel) == @size
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Encode a non-negative Integer to fixed-length Base58.
|
|
244
|
+
def i2s(int)
|
|
245
|
+
# Using divmod+setbyte is faster than Integer#digits under YJIT,
|
|
246
|
+
# and about equal in plain MRI.
|
|
247
|
+
alphabet = @alphabet
|
|
248
|
+
out = @blank.dup
|
|
249
|
+
idx = @size - 1
|
|
250
|
+
n = int
|
|
251
|
+
while idx >= 0 && n > 0
|
|
252
|
+
n, rem = n.divmod(58)
|
|
253
|
+
out.setbyte(idx, alphabet.getbyte(rem))
|
|
254
|
+
idx -= 1
|
|
255
|
+
end
|
|
256
|
+
out
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# Decode a fixed-length Base58 string to an Integer.
|
|
260
|
+
# Assumes the input passes `#valid?`.
|
|
261
|
+
def s2i(str)
|
|
262
|
+
# By unrolling coefficients, this is ~8x faster than Horner's scheme
|
|
263
|
+
#
|
|
264
|
+
# str.each_byte.inject(0) { _1 * 58 + @lut[_2] }
|
|
265
|
+
#
|
|
266
|
+
# at computing the inner product when using YJIT, by chunking
|
|
267
|
+
# intermediate results into 64-bit integers.
|
|
268
|
+
lut = @lut
|
|
269
|
+
|
|
270
|
+
acc0 = lut.getbyte(str.getbyte(0)) * 7427658739644928 +
|
|
271
|
+
lut.getbyte(str.getbyte(1)) * 128063081718016 +
|
|
272
|
+
lut.getbyte(str.getbyte(2)) * 2207984167552 +
|
|
273
|
+
lut.getbyte(str.getbyte(3)) * 38068692544 +
|
|
274
|
+
lut.getbyte(str.getbyte(4)) * 656356768 +
|
|
275
|
+
lut.getbyte(str.getbyte(5)) * 11316496 +
|
|
276
|
+
lut.getbyte(str.getbyte(6)) * 195112 +
|
|
277
|
+
lut.getbyte(str.getbyte(7)) * 3364 +
|
|
278
|
+
lut.getbyte(str.getbyte(8)) * 58 +
|
|
279
|
+
lut.getbyte(str.getbyte(9))
|
|
280
|
+
|
|
281
|
+
acc1 = lut.getbyte(str.getbyte(10)) * 7427658739644928 +
|
|
282
|
+
lut.getbyte(str.getbyte(11)) * 128063081718016 +
|
|
283
|
+
lut.getbyte(str.getbyte(12)) * 2207984167552 +
|
|
284
|
+
lut.getbyte(str.getbyte(13)) * 38068692544 +
|
|
285
|
+
lut.getbyte(str.getbyte(14)) * 656356768 +
|
|
286
|
+
lut.getbyte(str.getbyte(15)) * 11316496 +
|
|
287
|
+
lut.getbyte(str.getbyte(16)) * 195112 +
|
|
288
|
+
lut.getbyte(str.getbyte(17)) * 3364 +
|
|
289
|
+
lut.getbyte(str.getbyte(18)) * 58 +
|
|
290
|
+
lut.getbyte(str.getbyte(19))
|
|
291
|
+
|
|
292
|
+
lut.getbyte(str.getbyte(21)) +
|
|
293
|
+
58 * lut.getbyte(str.getbyte(20)) +
|
|
294
|
+
3364 * acc1 +
|
|
295
|
+
1449225352009601191936 * acc0
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: epithet
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Josh Goodall
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-12-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: minitest
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rdoc
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '7'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '7'
|
|
55
|
+
description: Epithet generates stable, prefixed, Base58 identifiers from 64-bit integers
|
|
56
|
+
using AES and HMAC.
|
|
57
|
+
email:
|
|
58
|
+
- inopinatus@hey.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- CHANGELOG.md
|
|
64
|
+
- LICENSE
|
|
65
|
+
- README.md
|
|
66
|
+
- SECURITY.md
|
|
67
|
+
- examples/basic.rb
|
|
68
|
+
- lib/epithet.rb
|
|
69
|
+
- lib/epithet/version.rb
|
|
70
|
+
homepage: https://github.com/inopinatus/epithet
|
|
71
|
+
licenses:
|
|
72
|
+
- MIT
|
|
73
|
+
metadata:
|
|
74
|
+
homepage_uri: https://inopinatus.github.io/epithet/
|
|
75
|
+
source_code_uri: https://github.com/inopinatus/epithet
|
|
76
|
+
changelog_uri: https://github.com/inopinatus/epithet/blob/main/CHANGELOG.md
|
|
77
|
+
bug_tracker_uri: https://github.com/inopinatus/epithet/issues
|
|
78
|
+
rubygems_mfa_required: 'true'
|
|
79
|
+
post_install_message:
|
|
80
|
+
rdoc_options: []
|
|
81
|
+
require_paths:
|
|
82
|
+
- lib
|
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
|
+
requirements:
|
|
85
|
+
- - ">="
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: '3'
|
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: '0'
|
|
93
|
+
requirements: []
|
|
94
|
+
rubygems_version: 3.0.3.1
|
|
95
|
+
signing_key:
|
|
96
|
+
specification_version: 4
|
|
97
|
+
summary: External base58 identifiers with reversible, authenticated obfuscation.
|
|
98
|
+
test_files: []
|