alt_characters 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +40 -5
- data/lib/alt32.rb +2 -29
- data/lib/alt_characters/alt32.rb +11 -0
- data/lib/alt_characters/alt_base.rb +40 -0
- data/lib/alt_characters/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 398b7281c3d04fcc95e7f582e6a3d015cf0de9f5005a1b98c25f67f54b1434a1
|
4
|
+
data.tar.gz: ae48022b091825de49fa68895e1a4370bd6741fd82de1370f2ec0d9a38d2678f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72b8a389ba131af60ae215e678844a839a81fe5cb9a598a31cde492efcea4a1f1175f1e63699a8bbcf8c91b117129d5ad7de74cf5b3e09cd8be15c0f449a1f73
|
7
|
+
data.tar.gz: 8d96e0351393d54918122fa95220a33fc3ea4c4a4c8dd7df0db00803f1028cd37298402729e9cea96e5ab23211baed0e9f33073cfdc5ef0574b2aeedc8053ad4
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# AltCharacters
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
alt_characters alternate base32 without numeric characters and padding
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,44 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
### Quick start
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'alt_characters'
|
27
|
+
|
28
|
+
encoded_text = AltCharacters.alt32_encode('foo')
|
29
|
+
# => "Nmhgw"
|
30
|
+
|
31
|
+
AltCharacters.alt32_decode(encoded_text)
|
32
|
+
# => "foo"
|
33
|
+
```
|
34
|
+
|
35
|
+
or
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'alt_characters'
|
39
|
+
|
40
|
+
encoded_text = Alt32.new.encode('foo')
|
41
|
+
# => "Nmhgw"
|
42
|
+
|
43
|
+
Alt32.new.decode(encoded_text)
|
44
|
+
# => "foo"
|
45
|
+
```
|
46
|
+
|
47
|
+
|
48
|
+
### Modifying encodable characters
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'alt_characters'
|
52
|
+
|
53
|
+
encodable_characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
|
54
|
+
|
55
|
+
encoded_text = Alt32.new(characters: encodable_characters).encode('foo')
|
56
|
+
# => "MZXW6"
|
57
|
+
|
58
|
+
Alt32.new(characters: encodable_characters).decode(encoded_text)
|
59
|
+
# => "foo"
|
60
|
+
```
|
26
61
|
|
27
62
|
## Development
|
28
63
|
|
@@ -32,7 +67,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
67
|
|
33
68
|
## Contributing
|
34
69
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/booink/alt_characters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
71
|
|
37
72
|
## License
|
38
73
|
|
data/lib/alt32.rb
CHANGED
@@ -1,30 +1,3 @@
|
|
1
|
-
|
2
|
-
DEFAULT_CHARACTERS = 'ABCDEFGHJKLMNPQRSTWXYfghkmprstwx'.freeze
|
1
|
+
require 'alt_characters/alt32'
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
def initialize(characters: DEFAULT_CHARACTERS)
|
7
|
-
@characters = characters.split(//) if characters.class == String
|
8
|
-
end
|
9
|
-
|
10
|
-
def encode(text)
|
11
|
-
binary = text.to_s.unpack("B*").first
|
12
|
-
binary.chars.each_slice(5).map do |chunk|
|
13
|
-
chunk << "0" * (5 - chunk.count) if chunk.count < 5
|
14
|
-
i = chunk.join.to_i(2)
|
15
|
-
@characters[i]
|
16
|
-
end.join
|
17
|
-
end
|
18
|
-
|
19
|
-
def decode(encoded_text)
|
20
|
-
binary = ""
|
21
|
-
encoded_text.to_s.chars.each do |character|
|
22
|
-
i = @characters.index(character)
|
23
|
-
raise DecodeError, "character(#{character}) does not exists" if i.nil?
|
24
|
-
binary += format("%05d", i.to_s(2))
|
25
|
-
end
|
26
|
-
string = [binary].pack("B*")
|
27
|
-
# string.encoding # => ASCII-8BIT
|
28
|
-
string.force_encoding("UTF-8").gsub("\x00", "")
|
29
|
-
end
|
30
|
-
end
|
3
|
+
Alt32 = AltCharacters::Alt32
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module AltCharacters
|
2
|
+
class AltBase
|
3
|
+
class DecodeError < StandardError; end
|
4
|
+
class EncodableCharactersError < StandardError; end
|
5
|
+
|
6
|
+
def initialize(length, characters: nil)
|
7
|
+
@length = length
|
8
|
+
@logarithm = Math.log2(length)
|
9
|
+
@characters = characters
|
10
|
+
@characters = characters.split(//) if characters.class == String
|
11
|
+
count = @characters.count
|
12
|
+
if count < length
|
13
|
+
raise EncodableCharactersError, "Not enough characters. #{count} characters specified"
|
14
|
+
elsif count > length
|
15
|
+
warn "[warn] Too many characters specified. The last #{count - length} characters are not used"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def encode(text)
|
20
|
+
binary = text.to_s.unpack("B*").first
|
21
|
+
binary.chars.each_slice(@logarithm).map do |chunk|
|
22
|
+
chunk << "0" * (@logarithm - chunk.count) if chunk.count < @logarithm
|
23
|
+
i = chunk.join.to_i(2)
|
24
|
+
@characters[i]
|
25
|
+
end.join
|
26
|
+
end
|
27
|
+
|
28
|
+
def decode(encoded_text)
|
29
|
+
binary = ""
|
30
|
+
encoded_text.to_s.chars.each do |character|
|
31
|
+
i = @characters.index(character)
|
32
|
+
raise DecodeError, "character(#{character}) does not exists" if i.nil?
|
33
|
+
binary += format("%0#{@logarithm}d", i.to_s(2))
|
34
|
+
end
|
35
|
+
string = [binary].pack("B*")
|
36
|
+
# string.encoding # => ASCII-8BIT
|
37
|
+
string.force_encoding("UTF-8").gsub("\x00", "")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alt_characters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- booink
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -75,6 +75,8 @@ files:
|
|
75
75
|
- exe/setup
|
76
76
|
- lib/alt32.rb
|
77
77
|
- lib/alt_characters.rb
|
78
|
+
- lib/alt_characters/alt32.rb
|
79
|
+
- lib/alt_characters/alt_base.rb
|
78
80
|
- lib/alt_characters/version.rb
|
79
81
|
homepage: https://github.com/booink/alt_characters
|
80
82
|
licenses:
|