base45 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/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +12 -0
- data/base45.gemspec +31 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/base45/encoding_table.rb +53 -0
- data/lib/base45/version.rb +5 -0
- data/lib/base45.rb +90 -0
- metadata +57 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 391fbba1f795babf955ab02b30fbe0f9016cd4440d33727bf76883feeb6a341d
|
|
4
|
+
data.tar.gz: c533882b3b041d2d5d5c5e6e68830900bc5ff63a42ea1c7c17b8d5867c5a7888
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 86c794ec6a105a7db8a7eab5d934d9bcccb2d6d58d38d75ab75fc2ff57047363f43fed0b025947fb92418b4c138b41e87e0b1ce1e42123d7fca3d4df1e285056
|
|
7
|
+
data.tar.gz: 937cad72b4c7b54d87f23eb8484a76a6589e029e3e8eb8afc0be86de63974f0c7bd44ecf15905d5c1253d32d3796c005cd6c20a9e0c19caba465afd0115f14df
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Wattswing
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Base45
|
|
2
|
+
|
|
3
|
+
The Base45 module provides for the encoding (#encode) and
|
|
4
|
+
decoding (#decode) of binary data using a Base45 representation.
|
|
5
|
+
|
|
6
|
+
Some resources used:
|
|
7
|
+
|
|
8
|
+
* [Base 45 Draft](https://datatracker.ietf.org/doc/draft-faltstrom-base45/)
|
|
9
|
+
* [A JS Base 45 implementation](http://base45-decode-encode.net/)
|
|
10
|
+
* [SO question](https://stackoverflow.com/questions/68114693/decode-base45-string)
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Add this line to your application's Gemfile:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
gem 'base45'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
And then execute:
|
|
21
|
+
|
|
22
|
+
$ bundle install
|
|
23
|
+
|
|
24
|
+
Or install it yourself as:
|
|
25
|
+
|
|
26
|
+
$ gem install base45
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Some simple encoding and decoding:
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
require "base45"
|
|
34
|
+
|
|
35
|
+
encoded = Base45.encode("The truth is out there")
|
|
36
|
+
# => "8UADZCKWE8%EG7D+EDN448%ES44+8DZKE"
|
|
37
|
+
|
|
38
|
+
decoded = Base45.decode(encoded)
|
|
39
|
+
# => "The truth is out there"
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Development
|
|
44
|
+
|
|
45
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
|
46
|
+
Then, run `rake spec` to run the tests.
|
|
47
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
48
|
+
|
|
49
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
50
|
+
To release a new version, update the version number in `version.rb`,
|
|
51
|
+
and then run `bundle exec rake release`, which will create a git tag
|
|
52
|
+
for the version, push git commits and the created tag, and push the `.gem`
|
|
53
|
+
file to [rubygems.org](https://rubygems.org).
|
|
54
|
+
|
|
55
|
+
## Contributing
|
|
56
|
+
|
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wattswing/base45.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/base45.gemspec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/base45/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "base45"
|
|
7
|
+
spec.version = Base45::VERSION
|
|
8
|
+
spec.authors = ["Wattswing"]
|
|
9
|
+
spec.email = ["wattswing@gmail.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "Decode / encode in base 45"
|
|
12
|
+
spec.description = spec.summary
|
|
13
|
+
|
|
14
|
+
spec.homepage = "https://github.com/wattswing/base45"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
spec.required_ruby_version = ">= 3.0.2"
|
|
17
|
+
|
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
20
|
+
|
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
25
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
spec.bindir = "exe"
|
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
30
|
+
spec.require_paths = ["lib"]
|
|
31
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "base45"
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require "irb"
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Base45
|
|
4
|
+
BASE_45_TABLE = {
|
|
5
|
+
"00" => "0",
|
|
6
|
+
"01" => "1",
|
|
7
|
+
"02" => "2",
|
|
8
|
+
"03" => "3",
|
|
9
|
+
"04" => "4",
|
|
10
|
+
"05" => "5",
|
|
11
|
+
"06" => "6",
|
|
12
|
+
"07" => "7",
|
|
13
|
+
"08" => "8",
|
|
14
|
+
"09" => "9",
|
|
15
|
+
"10" => "A",
|
|
16
|
+
"11" => "B",
|
|
17
|
+
"12" => "C",
|
|
18
|
+
"13" => "D",
|
|
19
|
+
"14" => "E",
|
|
20
|
+
"15" => "F",
|
|
21
|
+
"16" => "G",
|
|
22
|
+
"17" => "H",
|
|
23
|
+
"18" => "I",
|
|
24
|
+
"19" => "J",
|
|
25
|
+
"20" => "K",
|
|
26
|
+
"21" => "L",
|
|
27
|
+
"22" => "M",
|
|
28
|
+
"23" => "N",
|
|
29
|
+
"24" => "O",
|
|
30
|
+
"25" => "P",
|
|
31
|
+
"26" => "Q",
|
|
32
|
+
"27" => "R",
|
|
33
|
+
"28" => "S",
|
|
34
|
+
"29" => "T",
|
|
35
|
+
"30" => "U",
|
|
36
|
+
"31" => "V",
|
|
37
|
+
"32" => "W",
|
|
38
|
+
"33" => "X",
|
|
39
|
+
"34" => "Y",
|
|
40
|
+
"35" => "Z",
|
|
41
|
+
"36" => " ",
|
|
42
|
+
"37" => "$",
|
|
43
|
+
"38" => "%",
|
|
44
|
+
"39" => "*",
|
|
45
|
+
"40" => "+",
|
|
46
|
+
"41" => "-",
|
|
47
|
+
"42" => ".",
|
|
48
|
+
"43" => "/",
|
|
49
|
+
"44" => ":"
|
|
50
|
+
}.freeze
|
|
51
|
+
|
|
52
|
+
INVERTED_BASE_45_TABLE = BASE_45_TABLE.invert.freeze
|
|
53
|
+
end
|
data/lib/base45.rb
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# = base45.rb: methods for base45-encoding and -decoding strings
|
|
5
|
+
#
|
|
6
|
+
|
|
7
|
+
# The Base45 module provides for the encoding (#encode) and
|
|
8
|
+
# decoding (#decode) of binary data using a Base45 representation.
|
|
9
|
+
|
|
10
|
+
require_relative "base45/version"
|
|
11
|
+
require_relative "base45/encoding_table"
|
|
12
|
+
|
|
13
|
+
# Exposes two methods to decode and encode in base 45
|
|
14
|
+
module Base45
|
|
15
|
+
class Error < StandardError; end
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
# Returns the Base45-encoded version of +payload+
|
|
19
|
+
#
|
|
20
|
+
# require 'base45'
|
|
21
|
+
# Base45.encode("Encoding in base 45 !")
|
|
22
|
+
#
|
|
23
|
+
# <i>Generates:</i>
|
|
24
|
+
#
|
|
25
|
+
# :Y8UPCAVC3/DH44M-DUJCLQE934AW6X0
|
|
26
|
+
def encode(payload)
|
|
27
|
+
return if payload.length.zero?
|
|
28
|
+
|
|
29
|
+
return encode_for_single_char(payload) if payload.bytesize < 2
|
|
30
|
+
|
|
31
|
+
encode_for_multipe_chars(payload)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Returns the Base45-decoded version of +payload+
|
|
35
|
+
#
|
|
36
|
+
# require 'base45'
|
|
37
|
+
# Base45.encode(":Y8UPCAVC3/DH44M-DUJCLQE934AW6X0")
|
|
38
|
+
#
|
|
39
|
+
# <i>Generates:</i>
|
|
40
|
+
#
|
|
41
|
+
# Encoding in base 45 !
|
|
42
|
+
def decode(payload)
|
|
43
|
+
return if payload.length < 2
|
|
44
|
+
|
|
45
|
+
lookup = sliced_payload(payload)
|
|
46
|
+
base45_vals = lookup.map { |c, d, e| c + d * 45 + (e ? e * 45**2 : 0) }
|
|
47
|
+
|
|
48
|
+
base45_vals.pack("S>*").gsub(/\x00/, "")
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def sliced_payload(payload)
|
|
54
|
+
payload.chars.each_slice(3).map do |slice|
|
|
55
|
+
slice.map { |char| INVERTED_BASE_45_TABLE[char]&.to_i }
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def encode_for_single_char(payload)
|
|
60
|
+
keys = payload.bytes[0].divmod(45).reverse
|
|
61
|
+
|
|
62
|
+
keys.map { |i| BASE_45_TABLE[i.to_s.rjust(2, "0")] }.join
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def encode_for_multipe_chars(payload)
|
|
66
|
+
# 16-bit unsigned (unsigned char) - except big endian
|
|
67
|
+
bytes16 = payload.unpack("S>*")
|
|
68
|
+
|
|
69
|
+
is_odd = payload.bytesize.odd? && payload.bytes[-1] < 256
|
|
70
|
+
bytes16 << payload.bytes[-1] if is_odd
|
|
71
|
+
|
|
72
|
+
modulo45_bytes = modulo45_array(bytes16)
|
|
73
|
+
|
|
74
|
+
modulo45_bytes.flatten.map do |i|
|
|
75
|
+
BASE_45_TABLE[i.to_s.rjust(2, "0")]
|
|
76
|
+
end.join
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def modulo45_array(bytes16)
|
|
80
|
+
bytes16.map do |byte|
|
|
81
|
+
arr = []
|
|
82
|
+
multiplier, rest = byte.divmod(45**2)
|
|
83
|
+
arr << multiplier if multiplier.positive?
|
|
84
|
+
arr.push(*rest.divmod(45))
|
|
85
|
+
|
|
86
|
+
arr.reverse
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: base45
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wattswing
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-11-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Decode / encode in base 45
|
|
14
|
+
email:
|
|
15
|
+
- wattswing@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".rspec"
|
|
21
|
+
- ".rubocop.yml"
|
|
22
|
+
- Gemfile
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- Rakefile
|
|
26
|
+
- base45.gemspec
|
|
27
|
+
- bin/console
|
|
28
|
+
- bin/setup
|
|
29
|
+
- lib/base45.rb
|
|
30
|
+
- lib/base45/encoding_table.rb
|
|
31
|
+
- lib/base45/version.rb
|
|
32
|
+
homepage: https://github.com/wattswing/base45
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
metadata:
|
|
36
|
+
homepage_uri: https://github.com/wattswing/base45
|
|
37
|
+
source_code_uri: https://github.com/wattswing/base45
|
|
38
|
+
post_install_message:
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 3.0.2
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0'
|
|
52
|
+
requirements: []
|
|
53
|
+
rubygems_version: 3.2.29
|
|
54
|
+
signing_key:
|
|
55
|
+
specification_version: 4
|
|
56
|
+
summary: Decode / encode in base 45
|
|
57
|
+
test_files: []
|