tokenifier 0.0.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.
- data/README.md +63 -0
- data/Rakefile +11 -0
- data/bin/tokenifier +7 -0
- data/lib/generators/tokenifier/install/USAGE +11 -0
- data/lib/generators/tokenifier/install/install_generator.rb +22 -0
- data/lib/generators/tokenifier/install/templates/config/tokenifier.yml.erb +8 -0
- data/lib/tokenifier/cipher.rb +14 -0
- data/lib/tokenifier/cli.rb +79 -0
- data/lib/tokenifier/decrypt.rb +30 -0
- data/lib/tokenifier/encrypt.rb +22 -0
- data/lib/tokenifier/error.rb +3 -0
- data/lib/tokenifier/rails.rb +18 -0
- data/lib/tokenifier/random.rb +7 -0
- data/lib/tokenifier/version.rb +3 -0
- data/lib/tokenifier.rb +13 -0
- data/spec/cipher_spec.rb +51 -0
- data/spec/decrypt_spec.rb +71 -0
- data/spec/encrypt_spec.rb +81 -0
- data/spec/random_spec.rb +21 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/tekenifier_spec.rb +32 -0
- data/tokenifier.gemspec +19 -0
- metadata +122 -0
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,63 @@ | |
| 1 | 
            +
            # Tokenifier
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Tokenifier is a Gibberish gem wrapper.
         | 
| 4 | 
            +
            It provides an approach to encrypt and decrypt structures like Strings, Hashes.
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            ## Installation
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            Put the string into Gemfile
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                gem "tokenifier"
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            Add config/tokenifier.yml in rails application
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                development:
         | 
| 15 | 
            +
                  secret: 7e991d82a0dd42b0afa293a339308c6f
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            ## Usage
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            To encrypt data into hash
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                Tokenifier.encrypt("string")          # => 
         | 
| 22 | 
            +
                Tokenifier.encrypt(:key => 'value')   # => 
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            To decrypt data into hash
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                Tokenifier.decrypt(" ...")            # => 
         | 
| 27 | 
            +
                Tokenifier.decrypt(" ...")            # => 
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            Errors handling
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                Tokenifier.encrypt(nil)               # => raises Tokenifier::Error
         | 
| 32 | 
            +
                Tokenifier.encrypt("")                # => raises Tokenifier::Error
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                Tokenifier.decrypt("malformed hash")  # => raises Tokenifier::Error
         | 
| 35 | 
            +
             | 
| 36 | 
            +
            Using custom secret
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                data = Tokenifier.encrypt("string", :secret => 'secret')
         | 
| 39 | 
            +
                Tokenifier.decrypt(data, :secret => 'secret')               # => "string"
         | 
| 40 | 
            +
                Tokenifier.decrypt(data)                                    # => raises Tokenifier::Error, "Got a malformed string"
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            ## CLI usage
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            Usage:
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                tokenifier [options] COMMAND 'custom string'
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            Commands:
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                s|secret - Generates secret string
         | 
| 51 | 
            +
                e|encrypt - Does data encryption of any string data
         | 
| 52 | 
            +
                d|decrypt - Does data decryption from hashed data.
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                NOTE: You have to use permanent secret to decryption data.
         | 
| 55 | 
            +
                      Tokinifier generates dafult secret each execution time.
         | 
| 56 | 
            +
             | 
| 57 | 
            +
            Examples:
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                 tokenifier encrypt "CUSTOM DATA"
         | 
| 60 | 
            +
                 tokenifier decrypt "CUSTOM DATA"
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                 tokenifier --secret MYSECRET e "CUSTOM DATA"
         | 
| 63 | 
            +
                 tokenifier --secret MYSECRET d "ENCRYPTED DATA"
         | 
    
        data/Rakefile
    ADDED
    
    
    
        data/bin/tokenifier
    ADDED
    
    
| @@ -0,0 +1,11 @@ | |
| 1 | 
            +
            Description:
         | 
| 2 | 
            +
                Sets up Tokenifier in your Rails project. After running this generator you will
         | 
| 3 | 
            +
                get a new config/tokenifier.yml file with newly generated secrets.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                If you want to share tokens between applications use same secret strings
         | 
| 6 | 
            +
                in proper environment.
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            Examples:
         | 
| 9 | 
            +
                `rails generate tokenifier:install`
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                `rails generate tokenifier:install --help`
         | 
| @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            require 'rbconfig'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Tokenifier
         | 
| 4 | 
            +
              class InstallGenerator < ::Rails::Generators::Base
         | 
| 5 | 
            +
                source_root File.expand_path("../templates", __FILE__)
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def create_templates
         | 
| 8 | 
            +
                  template 'config/tokenifier.yml.erb', 'config/tokenifier.yml'
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                protected
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def embed_file(source, indent='')
         | 
| 14 | 
            +
                  IO.read(File.join(self.class.source_root, source)).gsub(/^/, indent)
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def embed_template(source, indent='')
         | 
| 18 | 
            +
                  template = File.join(self.class.source_root, source)
         | 
| 19 | 
            +
                  ERB.new(IO.read(template), nil, '-').result(binding).gsub(/^/, indent)
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
            end
         | 
| @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            module Tokenifier
         | 
| 2 | 
            +
              module Cipher
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                def secret
         | 
| 5 | 
            +
                  @secret ||= Tokenifier::Rails.secret || Tokenifier::Random.secret
         | 
| 6 | 
            +
                end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def cipher(*args, &block)
         | 
| 9 | 
            +
                  aes = Gibberish::AES.new(args.first || secret)
         | 
| 10 | 
            +
                  block_given? ? yield(aes) : aes
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
            end
         | 
| @@ -0,0 +1,79 @@ | |
| 1 | 
            +
            require 'optparse'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Tokenifier
         | 
| 4 | 
            +
              module Cli
         | 
| 5 | 
            +
                extend self
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def options
         | 
| 8 | 
            +
                  @options ||= {}
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def optparse
         | 
| 12 | 
            +
                  OptionParser.new do |opts|
         | 
| 13 | 
            +
                    opts.banner =<<-USAGE
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            Usage:
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                    tokenifier [options] COMMAND 'custom string'
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            Commands:
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                    s|secret - Generates secret string
         | 
| 22 | 
            +
                    e|encrypt - Does data encryption of any string data
         | 
| 23 | 
            +
                    d|decrypt - Does data decryption from hashed data.
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                    NOTE: You have to use permanent secret to decryption data.
         | 
| 26 | 
            +
                          Tokinifier generates dafult secret each execution time.
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            Examples:
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                    tokenifier encrypt "CUSTOM DATA"
         | 
| 31 | 
            +
                    tokenifier decrypt "CUSTOM DATA"
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                    tokenifier --secret MYSECRET e "CUSTOM DATA"
         | 
| 34 | 
            +
                    tokenifier --secret MYSECRET d "ENCRYPTED DATA"
         | 
| 35 | 
            +
             | 
| 36 | 
            +
             | 
| 37 | 
            +
            USAGE
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                    opts.on('-s', '--secret SECRET', 'Using custom secret phrase') do |secret|
         | 
| 40 | 
            +
                      options[:secret] = secret
         | 
| 41 | 
            +
                    end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                    opts.on('-h', '--help', 'Display this screen') do
         | 
| 44 | 
            +
                      puts opts
         | 
| 45 | 
            +
                      exit
         | 
| 46 | 
            +
                    end
         | 
| 47 | 
            +
                  end
         | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                def run
         | 
| 51 | 
            +
                  optparse.parse!
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  begin
         | 
| 54 | 
            +
                    case ARGV.first
         | 
| 55 | 
            +
                      when /^e/
         | 
| 56 | 
            +
                        if options[:secret].nil?
         | 
| 57 | 
            +
                          options[:secret] = Tokenifier::Random.secret
         | 
| 58 | 
            +
                          puts "SECRET: #{options[:secret]}"
         | 
| 59 | 
            +
                        end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                        puts Tokenifier.encrypt(ARGV.last, options)
         | 
| 62 | 
            +
                      when /^d/
         | 
| 63 | 
            +
                        puts Tokenifier.decrypt(ARGV.last, options)
         | 
| 64 | 
            +
                      when /^s/
         | 
| 65 | 
            +
                        puts Tokenifier::Random.secret
         | 
| 66 | 
            +
                      else
         | 
| 67 | 
            +
                        puts optparse.help
         | 
| 68 | 
            +
                        exit
         | 
| 69 | 
            +
                    end
         | 
| 70 | 
            +
                  rescue Tokenifier::Error => e
         | 
| 71 | 
            +
                    puts "ERROR: Token processing error."
         | 
| 72 | 
            +
                    puts optparse.help
         | 
| 73 | 
            +
                    exit
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              end
         | 
| 79 | 
            +
            end
         | 
| @@ -0,0 +1,30 @@ | |
| 1 | 
            +
            module Tokenifier
         | 
| 2 | 
            +
              module Decrypt
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                # data marshaling differs depending to ruby version
         | 
| 5 | 
            +
                # so its better to pack hash into string accoring to some rule
         | 
| 6 | 
            +
                def unpack_string(data)
         | 
| 7 | 
            +
                  case data
         | 
| 8 | 
            +
                    when /[\:(\#)?]/
         | 
| 9 | 
            +
                      hsh = data.split('#').map {|s| s.split(':')}.inject({}) {|c, (k, v)| c.merge({k => v})}
         | 
| 10 | 
            +
                      hsh.respond_to?(:with_indifferent_access) ? hsh.with_indifferent_access : hsh
         | 
| 11 | 
            +
                    else
         | 
| 12 | 
            +
                      data
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def decrypt(token, options = {})
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  raise Error, "Encrypted data should be a String" unless token.is_a?(String)
         | 
| 19 | 
            +
                  raise Error, "Got an incomlete encrypted string" if token.size < 12
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  cipher(options[:secret]) do |c|
         | 
| 22 | 
            +
                    unpack_string(c.dec(token))
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                rescue OpenSSL::Cipher::CipherError => e
         | 
| 26 | 
            +
                  raise Error, "Got a malformed string"
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
            end
         | 
| @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            module Tokenifier
         | 
| 2 | 
            +
              module Encrypt
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                # data marshaling differs depending to ruby version
         | 
| 5 | 
            +
                # so its better to pack hash into string accoring to some rule
         | 
| 6 | 
            +
                def pack_hash(hsh)
         | 
| 7 | 
            +
                  hsh.map { |v| v.join(':') }.join('#')
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def encrypt(data, options = {})
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  raise Tokenifier::Error, "DATA should not be nil" if data.nil?
         | 
| 13 | 
            +
                  raise Tokenifier::Error, "DATA should not be empty" if data.respond_to?(:empty?) && data.empty?
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  cipher(options[:secret]) do |c|
         | 
| 16 | 
            +
                    c.enc(data.is_a?(Hash) ? pack_hash(data) : data.to_s)
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
            end
         | 
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            if env_caller
         | 
| 2 | 
            +
              require 'rails'
         | 
| 3 | 
            +
              
         | 
| 4 | 
            +
              Rails.logger.info "I am required in rails"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              filename = File.expand_path(env_caller + "/../../config/tokenifier.yml")
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              if File.exists?(filename)
         | 
| 9 | 
            +
                Rails.logger.info YAML::load(
         | 
| 10 | 
            +
                  ERB.new(
         | 
| 11 | 
            +
                    IO.read(filename)
         | 
| 12 | 
            +
                  ).result
         | 
| 13 | 
            +
                )[Rails.env].inspect
         | 
| 14 | 
            +
              else
         | 
| 15 | 
            +
                Rails.logger.info "Config file not found"
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            end
         | 
    
        data/lib/tokenifier.rb
    ADDED
    
    | @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            require 'gibberish'
         | 
| 2 | 
            +
            require 'tokenifier/error'
         | 
| 3 | 
            +
            require 'tokenifier/random'
         | 
| 4 | 
            +
            require 'tokenifier/rails'
         | 
| 5 | 
            +
            require 'tokenifier/cipher'
         | 
| 6 | 
            +
            require 'tokenifier/encrypt'
         | 
| 7 | 
            +
            require 'tokenifier/decrypt'
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            module Tokenifier
         | 
| 10 | 
            +
              extend Cipher
         | 
| 11 | 
            +
              extend Encrypt
         | 
| 12 | 
            +
              extend Decrypt
         | 
| 13 | 
            +
            end
         | 
    
        data/spec/cipher_spec.rb
    ADDED
    
    | @@ -0,0 +1,51 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Tokenifier::Cipher do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              subject { Class.new.extend(described_class) }
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              describe ".cipher" do
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                context "calling method" do
         | 
| 10 | 
            +
                  specify { subject.cipher.should be_a(Gibberish::AES) }
         | 
| 11 | 
            +
                  specify { subject.cipher.cipher.should be_a(OpenSSL::Cipher::Cipher) }
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                context "passing block" do
         | 
| 15 | 
            +
                  specify { subject.cipher { |c| c.should_not be_nil } }
         | 
| 16 | 
            +
                  specify { subject.cipher { |c| c.should be_a(Gibberish::AES) } }
         | 
| 17 | 
            +
                  specify { subject.cipher { |c| c.cipher.should be_a(OpenSSL::Cipher::Cipher) } }
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                context "using encryption" do
         | 
| 21 | 
            +
                  let(:cipher) { subject.cipher }
         | 
| 22 | 
            +
                  specify { cipher.enc("string").should_not be_empty }
         | 
| 23 | 
            +
                  specify { cipher.dec(cipher.enc("string")).should == "string" }
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                context "custom secret" do
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  context "default secret" do
         | 
| 29 | 
            +
                    let(:first_call) { subject.cipher.password }
         | 
| 30 | 
            +
                    let(:second_call) { subject.cipher.password }
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                    specify { first_call.should == second_call }
         | 
| 33 | 
            +
                    specify { second_call.should == first_call }
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                  context "custom secret" do
         | 
| 37 | 
            +
                    let(:first_call) { subject.cipher("CUSTOM SECRET").password }
         | 
| 38 | 
            +
                    let(:second_call) { subject.cipher("CUSTOM SECRET").password }
         | 
| 39 | 
            +
                    let(:default) { subject.cipher.password }
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                    specify { default.should_not == second_call }
         | 
| 42 | 
            +
                    specify { first_call.should == second_call }
         | 
| 43 | 
            +
                    specify { second_call.should == first_call }
         | 
| 44 | 
            +
                    specify { second_call.should == "CUSTOM SECRET" }
         | 
| 45 | 
            +
                  end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
            end
         | 
| @@ -0,0 +1,71 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Tokenifier::Decrypt do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              subject { Class.new.extend(described_class) }
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              describe ".unpack_string" do
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                let(:unpacked) { subject.unpack_string(data) }
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                context "empty string" do
         | 
| 12 | 
            +
                  let(:data) { "" }
         | 
| 13 | 
            +
                  specify { unpacked.should == "" }
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                context "contains numeric value or string" do
         | 
| 17 | 
            +
                  let(:data) { "12345" }
         | 
| 18 | 
            +
                  specify { unpacked.should == "12345" }
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                context "contans packed hash" do
         | 
| 22 | 
            +
                  let(:data) { "a:1234567#d:d f#c:1.2.3" }
         | 
| 23 | 
            +
                  specify { unpacked.should == { "a" => "1234567", "c" => "1.2.3", "d" => "d f" } }
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                context "with_indifferent_access" do
         | 
| 27 | 
            +
                  let(:data) { "a:1234567#d:d f#c:1.2.3" }
         | 
| 28 | 
            +
                  let(:active_supported_hash) { mock(:active_supported_hash, :with_indifferent_access => "active_supported_hash") }
         | 
| 29 | 
            +
                  before { data.stub_chain(:split, :map, :inject).and_return(active_supported_hash) }
         | 
| 30 | 
            +
                  specify { unpacked.should == "active_supported_hash" }
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              describe ".decrypt" do
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                let(:cipher) { Proc.new{|*args, &block| yield(*args) } }
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                context "exception handing" do
         | 
| 40 | 
            +
                  before {
         | 
| 41 | 
            +
                    cipher.stub(:dec).and_return("some output")
         | 
| 42 | 
            +
                    subject.stub(:cipher).and_yield(cipher)
         | 
| 43 | 
            +
                  }
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  specify { lambda { subject.decrypt(nil) }.should raise_error(Tokenifier::Error) }
         | 
| 46 | 
            +
                  specify { lambda { subject.decrypt("") }.should  raise_error(Tokenifier::Error) }
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
                context "data types" do
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                  before {
         | 
| 53 | 
            +
                    cipher.should_receive(:dec).and_return(data)
         | 
| 54 | 
            +
                    subject.should_receive(:cipher).and_yield(cipher)
         | 
| 55 | 
            +
                  }
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                  context "String or Numeric" do
         | 
| 58 | 
            +
                    let(:data) { "123456789097" }
         | 
| 59 | 
            +
                    specify { subject.decrypt(data).should == "123456789097" }
         | 
| 60 | 
            +
                  end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                  context "Packed Hash" do
         | 
| 63 | 
            +
                    let(:data) { "a:123456789097#b:kjjdfr" }
         | 
| 64 | 
            +
                    specify { subject.decrypt(data).should == {"a" => "123456789097", "b" => "kjjdfr"} }
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
              end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
            end
         | 
| @@ -0,0 +1,81 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Tokenifier::Encrypt do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              subject { Class.new.extend(described_class) }
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              describe "#pack_hash" do
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                let(:packed) { subject.pack_hash(hsh) }
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                context "empty hash" do
         | 
| 12 | 
            +
                  let(:hsh) { {} }
         | 
| 13 | 
            +
                  let(:expected) { "" }
         | 
| 14 | 
            +
                  specify { packed.should == expected }
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                context "one key-value pair" do
         | 
| 18 | 
            +
                  let(:hsh) { {:a => 2} }
         | 
| 19 | 
            +
                  let(:expected) { "a:2" }
         | 
| 20 | 
            +
                  specify { packed.should == expected }
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                context "a few key-value pairs" do
         | 
| 24 | 
            +
                  let(:hsh) { {:a => 2, :b => 'string 123', :c => true} }
         | 
| 25 | 
            +
                  let(:expected) { "c:true#a:2#b:string 123" }
         | 
| 26 | 
            +
                  specify { packed.should == expected }
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                context "we are not supporting nested hashes yet due simplicity of solution" do
         | 
| 30 | 
            +
                  let(:hsh) { {:a => 2, :b => 'string 123', :sub => { :a => 33 } } }
         | 
| 31 | 
            +
                  let(:expected) { "sub:a33#a:2#b:string 123" }
         | 
| 32 | 
            +
                  specify { packed.should == expected }
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
              describe ".encrypt" do
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                let(:cipher) { Proc.new{|*args, &block| yield(*args) } }
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                context "exception handing" do
         | 
| 42 | 
            +
                  before {
         | 
| 43 | 
            +
                    cipher.stub(:enc).and_return("some output")
         | 
| 44 | 
            +
                    subject.stub(:cipher).and_yield(cipher)
         | 
| 45 | 
            +
                  }
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                  specify { lambda { subject.encrypt(nil) }.should raise_error(Tokenifier::Error) }
         | 
| 48 | 
            +
                  specify { lambda { subject.encrypt("") }.should  raise_error(Tokenifier::Error) }
         | 
| 49 | 
            +
                  specify { lambda { subject.encrypt({}) }.should  raise_error(Tokenifier::Error) }
         | 
| 50 | 
            +
                  specify { lambda { subject.encrypt(123) }.should_not  raise_error }
         | 
| 51 | 
            +
                  specify { lambda { subject.encrypt(1.1) }.should_not  raise_error }
         | 
| 52 | 
            +
                  specify { lambda { subject.encrypt("1") }.should_not  raise_error }
         | 
| 53 | 
            +
                  specify { lambda { subject.encrypt(:a => "1") }.should_not  raise_error }
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                context "data types" do
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                  before {
         | 
| 59 | 
            +
                    subject.should_receive(:cipher).and_yield(cipher)
         | 
| 60 | 
            +
                  }
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                  context "Numeric" do
         | 
| 63 | 
            +
                    before { cipher.should_receive(:enc).with("1").and_return("test output = 1") }
         | 
| 64 | 
            +
                    specify { subject.encrypt(1).should == "test output = 1" }
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                  context "String" do
         | 
| 68 | 
            +
                    before { cipher.should_receive(:enc).with("value").and_return("test output = value") }
         | 
| 69 | 
            +
                    specify { subject.encrypt("value").should == "test output = value" }
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                  context "Hash" do
         | 
| 73 | 
            +
                    before { cipher.should_receive(:enc).with("a:1234").and_return("test output = a:1234") }
         | 
| 74 | 
            +
                    specify { subject.encrypt(:a => 1234).should == "test output = a:1234" }
         | 
| 75 | 
            +
                  end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                end
         | 
| 78 | 
            +
             | 
| 79 | 
            +
              end
         | 
| 80 | 
            +
             | 
| 81 | 
            +
            end
         | 
    
        data/spec/random_spec.rb
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Tokenifier::Random do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              subject { described_class }
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              describe ".secret" do
         | 
| 8 | 
            +
                its(:secret) { should_not be_nil }
         | 
| 9 | 
            +
                its(:secret) { should be_a(String) }
         | 
| 10 | 
            +
                its(:secret) { should_not == "" }
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                context "uniqness" do
         | 
| 13 | 
            +
                  let(:first) { described_class.secret }
         | 
| 14 | 
            +
                  let(:second) { described_class.secret }
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  specify { first.should_not == second }
         | 
| 17 | 
            +
                  specify { second.should_not == first }
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            require 'tokenifier'
         | 
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Tokenifier do
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              it { should respond_to(:encrypt) }
         | 
| 6 | 
            +
              it { should respond_to(:decrypt) }
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              context "by default" do
         | 
| 9 | 
            +
                let(:data) { "some custom string" }
         | 
| 10 | 
            +
                let(:encrypted) { described_class.encrypt(data) }
         | 
| 11 | 
            +
                let(:decrypted) { described_class.decrypt(encrypted) }
         | 
| 12 | 
            +
                
         | 
| 13 | 
            +
                specify { decrypted.should == data }
         | 
| 14 | 
            +
                specify { data.should == decrypted }
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              context "custom secret" do
         | 
| 18 | 
            +
                let(:data) { "some custom string" }
         | 
| 19 | 
            +
                let(:secret) { "MY CUSTOM SECRET" }
         | 
| 20 | 
            +
                let(:encrypted) { described_class.encrypt(data, :secret => secret) }
         | 
| 21 | 
            +
                let(:decrypted) { described_class.decrypt(encrypted, :secret => secret) }
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                specify { lambda { described_class.decrypt(encrypted) }.should raise_error(Tokenifier::Error) }
         | 
| 24 | 
            +
                specify { decrypted.should == data }
         | 
| 25 | 
            +
                specify { data.should == decrypted }
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              context "rails support" do
         | 
| 29 | 
            +
                
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            end
         | 
    
        data/tokenifier.gemspec
    ADDED
    
    | @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            require 'lib/tokenifier/version'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Gem::Specification.new do |s|
         | 
| 4 | 
            +
              s.name          = 'tokenifier'
         | 
| 5 | 
            +
              s.version       = Tokenifier::VERSION
         | 
| 6 | 
            +
              s.authors       = ['Dmtiry Larkin']
         | 
| 7 | 
            +
              s.email         = ['dmitry.larkin@gmail.com']
         | 
| 8 | 
            +
              s.homepage      = 'https://github.com/ludo/tokenifier'
         | 
| 9 | 
            +
              s.summary       = 'Tokenifier Gem'
         | 
| 10 | 
            +
              s.description   = 'Tokenifier is a Gibberish gem wrapper. It provides an aproach to encrypt and decrypt structures like Strings, Hashes.'
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              s.add_runtime_dependency 'gibberish', '~>1.2'
         | 
| 13 | 
            +
              s.add_development_dependency 'rspec', '>= 2.6.0'
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              s.require_path  = 'lib'
         | 
| 16 | 
            +
              s.files         = Dir['{lib,spec,rails_generators}/**/*', 'bin/tokenifier', "[a-zA-Z]*"]
         | 
| 17 | 
            +
              s.test_files    = Dir['spec/**/*']
         | 
| 18 | 
            +
              s.executables   = ['tokenifier']
         | 
| 19 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,122 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification 
         | 
| 2 | 
            +
            name: tokenifier
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            +
              hash: 29
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
              segments: 
         | 
| 7 | 
            +
              - 0
         | 
| 8 | 
            +
              - 0
         | 
| 9 | 
            +
              - 1
         | 
| 10 | 
            +
              version: 0.0.1
         | 
| 11 | 
            +
            platform: ruby
         | 
| 12 | 
            +
            authors: 
         | 
| 13 | 
            +
            - Dmtiry Larkin
         | 
| 14 | 
            +
            autorequire: 
         | 
| 15 | 
            +
            bindir: bin
         | 
| 16 | 
            +
            cert_chain: []
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            date: 2011-12-23 00:00:00 Z
         | 
| 19 | 
            +
            dependencies: 
         | 
| 20 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 21 | 
            +
              name: gibberish
         | 
| 22 | 
            +
              prerelease: false
         | 
| 23 | 
            +
              requirement: &id001 !ruby/object:Gem::Requirement 
         | 
| 24 | 
            +
                none: false
         | 
| 25 | 
            +
                requirements: 
         | 
| 26 | 
            +
                - - ~>
         | 
| 27 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 28 | 
            +
                    hash: 11
         | 
| 29 | 
            +
                    segments: 
         | 
| 30 | 
            +
                    - 1
         | 
| 31 | 
            +
                    - 2
         | 
| 32 | 
            +
                    version: "1.2"
         | 
| 33 | 
            +
              type: :runtime
         | 
| 34 | 
            +
              version_requirements: *id001
         | 
| 35 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 36 | 
            +
              name: rspec
         | 
| 37 | 
            +
              prerelease: false
         | 
| 38 | 
            +
              requirement: &id002 !ruby/object:Gem::Requirement 
         | 
| 39 | 
            +
                none: false
         | 
| 40 | 
            +
                requirements: 
         | 
| 41 | 
            +
                - - ">="
         | 
| 42 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 43 | 
            +
                    hash: 23
         | 
| 44 | 
            +
                    segments: 
         | 
| 45 | 
            +
                    - 2
         | 
| 46 | 
            +
                    - 6
         | 
| 47 | 
            +
                    - 0
         | 
| 48 | 
            +
                    version: 2.6.0
         | 
| 49 | 
            +
              type: :development
         | 
| 50 | 
            +
              version_requirements: *id002
         | 
| 51 | 
            +
            description: Tokenifier is a Gibberish gem wrapper. It provides an aproach to encrypt and decrypt structures like Strings, Hashes.
         | 
| 52 | 
            +
            email: 
         | 
| 53 | 
            +
            - dmitry.larkin@gmail.com
         | 
| 54 | 
            +
            executables: 
         | 
| 55 | 
            +
            - tokenifier
         | 
| 56 | 
            +
            extensions: []
         | 
| 57 | 
            +
             | 
| 58 | 
            +
            extra_rdoc_files: []
         | 
| 59 | 
            +
             | 
| 60 | 
            +
            files: 
         | 
| 61 | 
            +
            - lib/generators/tokenifier/install/install_generator.rb
         | 
| 62 | 
            +
            - lib/generators/tokenifier/install/templates/config/tokenifier.yml.erb
         | 
| 63 | 
            +
            - lib/generators/tokenifier/install/USAGE
         | 
| 64 | 
            +
            - lib/tokenifier/cipher.rb
         | 
| 65 | 
            +
            - lib/tokenifier/cli.rb
         | 
| 66 | 
            +
            - lib/tokenifier/decrypt.rb
         | 
| 67 | 
            +
            - lib/tokenifier/encrypt.rb
         | 
| 68 | 
            +
            - lib/tokenifier/error.rb
         | 
| 69 | 
            +
            - lib/tokenifier/rails.rb
         | 
| 70 | 
            +
            - lib/tokenifier/random.rb
         | 
| 71 | 
            +
            - lib/tokenifier/version.rb
         | 
| 72 | 
            +
            - lib/tokenifier.rb
         | 
| 73 | 
            +
            - spec/cipher_spec.rb
         | 
| 74 | 
            +
            - spec/decrypt_spec.rb
         | 
| 75 | 
            +
            - spec/encrypt_spec.rb
         | 
| 76 | 
            +
            - spec/random_spec.rb
         | 
| 77 | 
            +
            - spec/spec_helper.rb
         | 
| 78 | 
            +
            - spec/tekenifier_spec.rb
         | 
| 79 | 
            +
            - bin/tokenifier
         | 
| 80 | 
            +
            - Rakefile
         | 
| 81 | 
            +
            - README.md
         | 
| 82 | 
            +
            - tokenifier.gemspec
         | 
| 83 | 
            +
            homepage: https://github.com/ludo/tokenifier
         | 
| 84 | 
            +
            licenses: []
         | 
| 85 | 
            +
             | 
| 86 | 
            +
            post_install_message: 
         | 
| 87 | 
            +
            rdoc_options: []
         | 
| 88 | 
            +
             | 
| 89 | 
            +
            require_paths: 
         | 
| 90 | 
            +
            - lib
         | 
| 91 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 92 | 
            +
              none: false
         | 
| 93 | 
            +
              requirements: 
         | 
| 94 | 
            +
              - - ">="
         | 
| 95 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 96 | 
            +
                  hash: 3
         | 
| 97 | 
            +
                  segments: 
         | 
| 98 | 
            +
                  - 0
         | 
| 99 | 
            +
                  version: "0"
         | 
| 100 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 101 | 
            +
              none: false
         | 
| 102 | 
            +
              requirements: 
         | 
| 103 | 
            +
              - - ">="
         | 
| 104 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 105 | 
            +
                  hash: 3
         | 
| 106 | 
            +
                  segments: 
         | 
| 107 | 
            +
                  - 0
         | 
| 108 | 
            +
                  version: "0"
         | 
| 109 | 
            +
            requirements: []
         | 
| 110 | 
            +
             | 
| 111 | 
            +
            rubyforge_project: 
         | 
| 112 | 
            +
            rubygems_version: 1.8.10
         | 
| 113 | 
            +
            signing_key: 
         | 
| 114 | 
            +
            specification_version: 3
         | 
| 115 | 
            +
            summary: Tokenifier Gem
         | 
| 116 | 
            +
            test_files: 
         | 
| 117 | 
            +
            - spec/cipher_spec.rb
         | 
| 118 | 
            +
            - spec/decrypt_spec.rb
         | 
| 119 | 
            +
            - spec/encrypt_spec.rb
         | 
| 120 | 
            +
            - spec/random_spec.rb
         | 
| 121 | 
            +
            - spec/spec_helper.rb
         | 
| 122 | 
            +
            - spec/tekenifier_spec.rb
         |