encrypted_text 0.1 → 0.1.1

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.
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  DEPS = {
23
23
  'fast-aes' => '~> 0.1',
24
24
  'hex_string' => '~> 1.0',
25
- 'all-your-base' => '~> 0.3',
25
+ 'radix' => '~> 2.0.1',
26
26
  }
27
27
 
28
28
  DEPS.each do |lib, version|
@@ -1,6 +1,6 @@
1
- require 'fast-aes'
1
+ require 'fast-aes' # Encryption engine
2
2
  require 'hex_string' # Encryption engines typically return binary, so we need to convert to hex
3
- require 'all_your_base/are/belong_to_us' # Allows us to compress to high-base character strings
3
+ require 'radix' # Base conversion
4
4
 
5
5
  require_relative 'errs'
6
6
 
@@ -30,35 +30,42 @@ module EncryptedText
30
30
 
31
31
  def charset=(charset)
32
32
  @charset = charset
33
- @base_converter = AllYourBase::Are.new(:charset => @charset, :radix => @charset.size)
34
-
35
- @charset
33
+ @reverse_charset = @charset.each.with_index.reduce({}) do |res, (c, i)|
34
+ res[c] = i
35
+ res
36
+ end
36
37
  end
37
38
 
38
39
  def key=(key)
39
40
  @key = key
40
41
  @engine = FastAES.new(@key)
41
-
42
42
  @key
43
43
  end
44
44
 
45
45
  def salt_size=(s)
46
- raise ArgumentError("Salt size must be integer") unless s.is_a?(Integer)
46
+ raise ArgumentError.new("Salt size must be integer") unless s.is_a?(Integer)
47
47
  @salt_size = s
48
48
  end
49
49
 
50
+ def base_encode(val, from_base)
51
+ val.b(from_base).to_a.map{ |id| @charset[id] }.join
52
+ end
53
+
54
+ def base_decode(val)
55
+ val.split.map{ |c| @reverse_charset.fetch(c) }.b(@charset.size).to_i
56
+ end
57
+
50
58
  def encode(message)
51
59
  salt = (0...@salt_size).map { @charset.sample }.join
52
60
  signed = @signature + salt + message
53
61
  encrypted = @engine.encrypt(signed)
54
- hex_string = encrypted.to_hex_string.split(' ').join
55
- hex = ("1" + hex_string).hex # Add "1" prefix in case hex_string has leading zeroes
56
- encoded = @base_converter.convert_from_base_10(hex.to_i)
62
+ hex_string = '1' + encrypted.to_hex_string.split(' ').join # Add "1" prefix in case hex_string has leading zeroes
63
+ encoded = Radix.convert(hex_string.upcase, 16, 62) # Radix requires allcaps
57
64
  end
58
65
 
59
66
  def decode(encoded)
60
67
  begin
61
- hex_string = @base_converter.convert_to_base_10(encoded).to_base_16
68
+ hex_string = Radix.convert(encoded, 62, 16)
62
69
  hex_string = hex_string[1..-1] if hex_string[0] == '1' # remove "1" prefix
63
70
  hex_string = "0" + hex_string if (hex_string.size % 2) != 0 # Make sure we have an even number of hex digits
64
71
  byte_string = hex_string.to_byte_string
@@ -1,3 +1,3 @@
1
1
  module EncryptedText
2
- VERSION = '0.1'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encrypted_text
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -44,13 +44,13 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '1.0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: all-your-base
47
+ name: radix
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '0.3'
53
+ version: 2.0.1
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0.3'
61
+ version: 2.0.1
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rspec
64
64
  requirement: !ruby/object:Gem::Requirement