diff options
| author | mo <mo.khan@gmail.com> | 2018-06-13 15:57:37 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2018-06-13 15:57:37 -0600 |
| commit | 28004c638852f86160a01b61294ea831252f076e (patch) | |
| tree | d1e86738583b42dcf72fb0fe48b5f0903912eec3 | |
| parent | ba175991cf05cc310586606788c2466acdac0ef1 (diff) | |
split classes into separate files.
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | lib/trunk.rb | 3 | ||||
| -rw-r--r-- | lib/trunk/serializers/base_64.rb | 13 | ||||
| -rw-r--r-- | lib/trunk/serializers/composite.rb | 27 | ||||
| -rw-r--r-- | lib/trunk/serializers/crypto.rb | 29 | ||||
| -rw-r--r-- | lib/trunk/storage.rb | 62 | ||||
| -rw-r--r-- | spec/storage_spec.rb | 6 | ||||
| -rw-r--r-- | trunk.gemspec | 4 |
8 files changed, 78 insertions, 70 deletions
@@ -22,8 +22,6 @@ Or install it yourself as: ## Usage -TODO: Write usage instructions here - ```bash $ trunk add key password $ trunk add key - # read password from stdin @@ -45,7 +43,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/trunk. +Bug reports and pull requests are welcome on GitHub at https://github.com/mokhan/trunk. ## License diff --git a/lib/trunk.rb b/lib/trunk.rb index 95c35c7..ac975dc 100644 --- a/lib/trunk.rb +++ b/lib/trunk.rb @@ -1,6 +1,9 @@ require 'thor' require "trunk/cli" +require "trunk/serializers/base_64" +require "trunk/serializers/composite" +require "trunk/serializers/crypto" require "trunk/storage" require "trunk/version" diff --git a/lib/trunk/serializers/base_64.rb b/lib/trunk/serializers/base_64.rb new file mode 100644 index 0000000..a67804b --- /dev/null +++ b/lib/trunk/serializers/base_64.rb @@ -0,0 +1,13 @@ +module Trunk + module Serializers + class Base64 + def serialize(value) + ::Base64.strict_encode64(value) + end + + def deserialize(value) + ::Base64.decode64(value) + end + end + end +end diff --git a/lib/trunk/serializers/composite.rb b/lib/trunk/serializers/composite.rb new file mode 100644 index 0000000..f362807 --- /dev/null +++ b/lib/trunk/serializers/composite.rb @@ -0,0 +1,27 @@ +module Trunk + module Serializers + class Composite + def initialize(serializers = []) + @serializers = serializers + end + + def add(serializer) + @serializers.push(serializer) + end + + def serialize(value) + @serializers.each do |x| + value = x.serialize(value) + end + value + end + + def deserialize(value) + @serializers.reverse.each do |x| + value = x.deserialize(value) + end + value + end + end + end +end diff --git a/lib/trunk/serializers/crypto.rb b/lib/trunk/serializers/crypto.rb new file mode 100644 index 0000000..a374cd2 --- /dev/null +++ b/lib/trunk/serializers/crypto.rb @@ -0,0 +1,29 @@ +module Trunk + module Serializers + class SymmetricCrypto + attr_reader :private_key, :algorithm + + def initialize(private_key, algorithm: 'AES-256-CBC') + @private_key = private_key + @algorithm = algorithm + end + + def serialize(plain_text) + cipher = OpenSSL::Cipher.new(algorithm) + cipher.encrypt + cipher.key = private_key + cipher.random_iv + cipher.update(plain_text) + cipher.final + end + + def deserialize(cipher_text) + cipher = OpenSSL::Cipher.new(algorithm) + cipher.decrypt + iv = cipher_text[0..cipher.iv_len - 1] + data = cipher_text[cipher.iv_len..-1] + cipher.key = private_key + cipher.iv = iv + cipher.update(data) + cipher.final + end + end + end +end diff --git a/lib/trunk/storage.rb b/lib/trunk/storage.rb index a041f60..7adaa35 100644 --- a/lib/trunk/storage.rb +++ b/lib/trunk/storage.rb @@ -1,66 +1,4 @@ module Trunk - class CompositeSerializer - def initialize(serializers = []) - @serializers = serializers - end - - def add(serializer) - @serializers.push(serializer) - end - - def serialize(value) - @serializers.each do |x| - value = x.serialize(value) - end - value - end - - def deserialize(value) - puts "deserialize" - @serializers.reverse.each do |x| - puts x.class - value = x.deserialize(value) - end - value - end - end - - class Base64Serializer - def serialize(value) - Base64.strict_encode64(value) - end - - def deserialize(value) - Base64.decode64(value) - end - end - - class SymmetricCrypto - attr_reader :private_key, :algorithm - - def initialize(private_key, algorithm: 'AES-256-CBC') - @private_key = private_key - @algorithm = algorithm - end - - def serialize(plain_text) - cipher = OpenSSL::Cipher.new(algorithm) - cipher.encrypt - cipher.key = private_key - cipher.random_iv + cipher.update(plain_text) + cipher.final - end - - def deserialize(cipher_text) - cipher = OpenSSL::Cipher.new(algorithm) - cipher.decrypt - iv = cipher_text[0..cipher.iv_len - 1] - data = cipher_text[cipher.iv_len..-1] - cipher.key = private_key - cipher.iv = iv - cipher.update(data) + cipher.final - end - end - class Storage def initialize(hash, serializer) @hash = hash diff --git a/spec/storage_spec.rb b/spec/storage_spec.rb index 04cb7e2..e742e2a 100644 --- a/spec/storage_spec.rb +++ b/spec/storage_spec.rb @@ -3,9 +3,9 @@ require 'spec_helper' RSpec.describe Trunk::Storage do let(:crypto) { Crypto.new('aes256-cbc', key) } let(:serializer) do - x = Trunk::CompositeSerializer.new - x.add(Trunk::SymmetricCrypto.new(key)) - x.add(Trunk::Base64Serializer.new) + x = Trunk::Serializers::Composite.new + x.add(Trunk::Serializers::SymmetricCrypto.new(key)) + x.add(Trunk::Serializers::Base64.new) x end diff --git a/trunk.gemspec b/trunk.gemspec index bce1f40..e923161 100644 --- a/trunk.gemspec +++ b/trunk.gemspec @@ -6,8 +6,8 @@ require "trunk/version" Gem::Specification.new do |spec| spec.name = "trunk" spec.version = Trunk::VERSION - spec.authors = ["mokha"] - spec.email = ["mokha@cisco.com"] + spec.authors = ["mo"] + spec.email = ["mo@mokhan.ca"] spec.summary = %q{A safe place to put things.} spec.description = %q{A safe place to put things.} |
