Skip to content

Commit

Permalink
Updated to work with ruby 2.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
oteyatosys committed Aug 21, 2018
1 parent fafe4e9 commit 118218a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Enigma
AionEnigma
======

Enigma is a small utility for encrypting/decrypting a string based on a shared secret using AES-265-CBC.
AionEnigma is a small utility for encrypting/decrypting a string based on a shared secret using AES-265-CBC.

Install
-------
Expand All @@ -18,9 +18,9 @@ Usage
secret_message = 'this is a secret message'
shared_secret = 'some shared secret'

enigma = Enigma.new(shared_secret)
enigma = AionEnigma.new(shared_secret)
encrypted_message = enigma.encrypt(secret_message)
# encrypted_message ≈> 'Gn3AZKG9aqv+ALTfI9ZbuQ==|7hI5iN1Jdm73zQB1nTBngX07SaX60nuirWQRtNygIgE='
# encrypted_message ≈> 'ivBsGDsQjG6ScC5wq7Q-2w~XgFz1c4mdDR_MhI0VkpvNMcINDHCrAXEb1RlzwXpuNU'

message = enigma.decrypt(encrypted_message)
# message => 'this is a secret message'
Expand Down
2 changes: 1 addition & 1 deletion lib/aion-enigma.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'enigma'
require 'aion_enigma'
8 changes: 4 additions & 4 deletions lib/enigma.rb → lib/aion_enigma.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'openssl'
require 'base64'

class Enigma
class AionEnigma

class EncryptError < StandardError; end
class DecryptError < StandardError; end
Expand All @@ -12,7 +12,7 @@ def initialize(secret)
unless secret.is_a? String
fail ArgumentError, 'secret must be a string'
end
@key = Digest::SHA1.hexdigest(secret)
@key = Digest::SHA2.digest(secret)
end

# Encrypts a string.
Expand Down Expand Up @@ -89,7 +89,7 @@ def decrypt(encrypted_message)
# @param encrypted the encrypted bytes
# @return String with serialized iv and encrypted parameters
def pack(iv, encrypted)
[iv, encrypted].map { |x| Base64.encode64(x).strip }.join('|')
[iv, encrypted].map { |x| Base64.urlsafe_encode64(x).gsub('=','') }.join('~')
end

# Unpacking the encrypted_message into an array of iv and encrypted data.
Expand All @@ -103,7 +103,7 @@ def pack(iv, encrypted)
# @param encrypted_message as outputted by Enigma#pack
# @return Array containing iv bytes and encrypted bytes
def unpack(encrypted_message)
encrypted_message.split('|').map { |m| Base64.decode64(m) }
encrypted_message.split('~').map { |m| Base64.urlsafe_decode64(m) }
end

end
Expand Down
22 changes: 11 additions & 11 deletions test/enigma_test.rb → test/aion_enigma_test.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require 'minitest/autorun'
require 'enigma'
require 'aion_enigma'

class EnigmaTest < Minitest::Test
class AionEnigmaTest < Minitest::Test

def setup
@secret = 'some shared secret'
@enigma = Enigma.new(@secret)
@enigma = AionEnigma.new(@secret)
end

def test_non_string_secret
assert_raises ArgumentError do
Enigma.new({})
AionEnigma.new({})
end
end

Expand Down Expand Up @@ -50,8 +50,8 @@ def test_decrypt
def test_other_enigma
secret = 'shared secret for testing 123'

enigma1 = Enigma.new(secret)
enigma2 = Enigma.new(secret)
enigma1 = AionEnigma.new(secret)
enigma2 = AionEnigma.new(secret)

message_in = 'this is a string to encrypt'
encrypted_message = enigma1.encrypt(message_in)
Expand All @@ -61,13 +61,13 @@ def test_other_enigma
end

def test_secret_mismatch
enigma1 = Enigma.new('secret 1')
enigma2 = Enigma.new('secret 2')
enigma1 = AionEnigma.new('secret 1')
enigma2 = AionEnigma.new('secret 2')

message_in = 'testing mismatch'
encrypted_message = enigma1.encrypt(message_in)

assert_raises Enigma::DecryptError do
assert_raises AionEnigma::DecryptError do
enigma2.decrypt(encrypted_message)
end
end
Expand All @@ -76,9 +76,9 @@ def test_readme_example
secret_message = 'this is a secret message'
shared_secret = 'some shared secret'

enigma = Enigma.new(shared_secret)
enigma = AionEnigma.new(shared_secret)
encrypted_message = enigma.encrypt(secret_message)
# encrypted_message ≈> 'Gn3AZKG9aqv+ALTfI9ZbuQ==|7hI5iN1Jdm73zQB1nTBngX07SaX60nuirWQRtNygIgE='
# encrypted_message ≈> 'ivBsGDsQjG6ScC5wq7Q-2w~XgFz1c4mdDR_MhI0VkpvNMcINDHCrAXEb1RlzwXpuNU'

message = enigma.decrypt(encrypted_message)
# message => 'this is a secret message'
Expand Down

0 comments on commit 118218a

Please sign in to comment.