id_cipher 0.0.1 → 0.1.0
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.
- checksums.yaml +4 -4
 - data/README.md +27 -5
 - data/id_cipher.gemspec +3 -2
 - data/lib/id_cipher.rb +17 -8
 - data/spec/id_cipher_spec.rb +32 -0
 - metadata +10 -9
 - data/lib/id_cipher/version.rb +0 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 18ba804318b65f79e23788ccb72b5f22e968e6d9
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: cb6afe17b901c11990cc1bb129055b5ac581a018
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 115201215dfad785f5c93e50987cb5aa9b173129c12cf4a89769c476df139219dd8e44372b26c57b6df48c18f06c2060d3fe86c2e3dd29f54bf0f059fdddfb7b
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 35cb57637414f4b612584df85e60815d9a1ce556272f01f02e2ff13449ae7314dde06d5c9d0103bbf87e74d0412c719e00b5962118f10acb034d2bdbb9f104e8
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # IdCipher
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            cipher Integer to another Integer, make  
     | 
| 
      
 3 
     | 
    
         
            +
            cipher Integer to another Integer, make customer confused with the real number
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
            ## Installation
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
         @@ -18,12 +18,34 @@ Or install it yourself as: 
     | 
|
| 
       18 
18 
     | 
    
         | 
| 
       19 
19 
     | 
    
         
             
            ## Usage
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
            set  
     | 
| 
      
 21 
     | 
    
         
            +
            set cipher slat in two ways:
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
            IdCipher.decrypt(42491238512) # puts 1
         
     | 
| 
      
 23 
     | 
    
         
            +
            1. if use this in ruby on rails, add config in ``settings.yml``:
         
     | 
| 
       25 
24 
     | 
    
         | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
      
 25 
     | 
    
         
            +
                    Settings.id_cipher_key = 'something.like.this'
         
     | 
| 
      
 26 
     | 
    
         
            +
                    
         
     | 
| 
      
 27 
     | 
    
         
            +
            2. or set IdCipher::KEY direct
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                    module IdCipher
         
     | 
| 
      
 30 
     | 
    
         
            +
                        KEY = 'something.like.this'
         
     | 
| 
      
 31 
     | 
    
         
            +
                    end
         
     | 
| 
      
 32 
     | 
    
         
            +
                    
         
     | 
| 
      
 33 
     | 
    
         
            +
            notice: the key length must be over 16, should not longer than 256.
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
            #### code example
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
            > IdCipher.encrypt(1) # puts 42491238512
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
            >IdCipher.decrypt(42491238512) # puts 1
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
            **the result about encrypt is depend on key**
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
            IdCipher has two method, most of time, result of two method will be same.
         
     | 
| 
      
 44 
     | 
    
         
            +
            but **DO NOT** use them obscurely.
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
            ID cipher use C ``pack`` function, so it has up limmit for id,
         
     | 
| 
      
 47 
     | 
    
         
            +
            ``pack`` function use 'L' type pack id, it's a unsigned long type, 
         
     | 
| 
      
 48 
     | 
    
         
            +
            value range is 0~(2**32 - 1), means it must less than 4294967295
         
     | 
| 
       27 
49 
     | 
    
         | 
| 
       28 
50 
     | 
    
         
             
            ## Contributing
         
     | 
| 
       29 
51 
     | 
    
         | 
    
        data/id_cipher.gemspec
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # coding: utf-8
         
     | 
| 
       2 
2 
     | 
    
         
             
            lib = File.expand_path('../lib', __FILE__)
         
     | 
| 
       3 
3 
     | 
    
         
             
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         
     | 
| 
       4 
     | 
    
         
            -
            require 'id_cipher 
     | 
| 
      
 4 
     | 
    
         
            +
            require 'id_cipher'
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            Gem::Specification.new do |spec|
         
     | 
| 
       7 
7 
     | 
    
         
             
              spec.name          = "id_cipher"
         
     | 
| 
         @@ -11,6 +11,7 @@ Gem::Specification.new do |spec| 
     | 
|
| 
       11 
11 
     | 
    
         
             
              spec.summary       = %q{cipher integer to another integer}
         
     | 
| 
       12 
12 
     | 
    
         
             
              spec.description   = %q{only used to cipher integer, use in confused with ID}
         
     | 
| 
       13 
13 
     | 
    
         
             
              spec.homepage      = "https://github.com/delongw/ruby-id-cipher"
         
     | 
| 
      
 14 
     | 
    
         
            +
              spec.required_ruby_version = '>= 2.0'
         
     | 
| 
       14 
15 
     | 
    
         
             
              spec.license       = "MIT"
         
     | 
| 
       15 
16 
     | 
    
         | 
| 
       16 
17 
     | 
    
         
             
              spec.files         = `git ls-files -z`.split("\x0")
         
     | 
| 
         @@ -19,5 +20,5 @@ Gem::Specification.new do |spec| 
     | 
|
| 
       19 
20 
     | 
    
         
             
              spec.require_paths = ["lib"]
         
     | 
| 
       20 
21 
     | 
    
         | 
| 
       21 
22 
     | 
    
         
             
              spec.add_development_dependency "bundler", "~> 1.6"
         
     | 
| 
       22 
     | 
    
         
            -
              spec.add_development_dependency "rake"
         
     | 
| 
      
 23 
     | 
    
         
            +
              spec.add_development_dependency "rake", "~> 10.4"
         
     | 
| 
       23 
24 
     | 
    
         
             
            end
         
     | 
    
        data/lib/id_cipher.rb
    CHANGED
    
    | 
         @@ -1,5 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require 'openssl'
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            module IdCipher
         
     | 
| 
      
 5 
     | 
    
         
            +
              VERSION = "0.1.0"
         
     | 
| 
      
 6 
     | 
    
         
            +
            end
         
     | 
| 
       3 
7 
     | 
    
         | 
| 
       4 
8 
     | 
    
         | 
| 
       5 
9 
     | 
    
         
             
            module IdCipher
         
     | 
| 
         @@ -25,29 +29,34 @@ module IdCipher 
     | 
|
| 
       25 
29 
     | 
    
         | 
| 
       26 
30 
     | 
    
         
             
                def initialize
         
     | 
| 
       27 
31 
     | 
    
         
             
                  @cipher = OpenSSL::Cipher::Cipher.new('rc4')
         
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                def key
         
     | 
| 
      
 35 
     | 
    
         
            +
                  (defined? Setting) && Setting.id_cipher_key ||
         
     | 
| 
      
 36 
     | 
    
         
            +
                      IdCipher::KEY ||
         
     | 
| 
      
 37 
     | 
    
         
            +
                      (raise TypeError, 'no cipher key found. you can set key within Settings.yml, id_cipher_key="cipher slat" or refine IdCipher::KEY')
         
     | 
| 
       29 
38 
     | 
    
         
             
                end
         
     | 
| 
       30 
39 
     | 
    
         | 
| 
       31 
40 
     | 
    
         
             
                def pack(number)
         
     | 
| 
       32 
     | 
    
         
            -
                  raise TypeError, "#{number} is not a Fixnum" if number.class != Fixnum
         
     | 
| 
       33 
41 
     | 
    
         
             
                  [number].pack('L')
         
     | 
| 
       34 
42 
     | 
    
         
             
                end
         
     | 
| 
       35 
43 
     | 
    
         | 
| 
       36 
44 
     | 
    
         
             
                def unpack(crypto_text)
         
     | 
| 
       37 
     | 
    
         
            -
                  raise TypeError, "#{crypto_text} is not a String" if crypto_text.class != String
         
     | 
| 
       38 
45 
     | 
    
         
             
                  crypto_text.unpack('L')[0]
         
     | 
| 
       39 
46 
     | 
    
         
             
                end
         
     | 
| 
       40 
47 
     | 
    
         | 
| 
       41 
48 
     | 
    
         
             
                def encrypt(id)
         
     | 
| 
      
 49 
     | 
    
         
            +
                  id = id.to_i
         
     | 
| 
       42 
50 
     | 
    
         
             
                  @cipher.encrypt
         
     | 
| 
       43 
     | 
    
         
            -
                  @cipher.key =  
     | 
| 
      
 51 
     | 
    
         
            +
                  @cipher.key = key
         
     | 
| 
       44 
52 
     | 
    
         
             
                  self.unpack @cipher.update(self.pack(id))
         
     | 
| 
       45 
53 
     | 
    
         
             
                end
         
     | 
| 
       46 
54 
     | 
    
         | 
| 
       47 
     | 
    
         
            -
                def decrypt( 
     | 
| 
      
 55 
     | 
    
         
            +
                def decrypt(eid)
         
     | 
| 
      
 56 
     | 
    
         
            +
                  eid = eid.to_i
         
     | 
| 
       48 
57 
     | 
    
         
             
                  @cipher.decrypt
         
     | 
| 
       49 
     | 
    
         
            -
                  @cipher.key =  
     | 
| 
       50 
     | 
    
         
            -
                  self.unpack @cipher.update(self.pack( 
     | 
| 
      
 58 
     | 
    
         
            +
                  @cipher.key = key
         
     | 
| 
      
 59 
     | 
    
         
            +
                  self.unpack @cipher.update(self.pack(eid))
         
     | 
| 
       51 
60 
     | 
    
         
             
                end
         
     | 
| 
       52 
61 
     | 
    
         | 
| 
       53 
62 
     | 
    
         
             
              end
         
     | 
| 
         @@ -0,0 +1,32 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'minitest/autorun'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require_relative '../lib/id_cipher.rb'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            module IdCipher
         
     | 
| 
      
 6 
     | 
    
         
            +
              KEY = '0123456789abcdef'
         
     | 
| 
      
 7 
     | 
    
         
            +
            end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            class TestCipher < MiniTest::Unit::TestCase
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
              def setup
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
              end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
              def test_encrypt
         
     | 
| 
      
 17 
     | 
    
         
            +
                assert_equal 1497393285, IdCipher.encrypt(1)
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
              def test_decrypt
         
     | 
| 
      
 21 
     | 
    
         
            +
                assert_equal 1, IdCipher.decrypt(1497393285)
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
              def test_encrypt_with_string
         
     | 
| 
      
 25 
     | 
    
         
            +
                assert_equal 1497393407, IdCipher.encrypt('123')
         
     | 
| 
      
 26 
     | 
    
         
            +
              end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
              def test_decrypt_with_string
         
     | 
| 
      
 29 
     | 
    
         
            +
                assert_equal 123, IdCipher.encrypt('1497393407')
         
     | 
| 
      
 30 
     | 
    
         
            +
              end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: id_cipher
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.0 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - delong
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date:  
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2015-02-25 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: bundler
         
     | 
| 
         @@ -28,16 +28,16 @@ dependencies: 
     | 
|
| 
       28 
28 
     | 
    
         
             
              name: rake
         
     | 
| 
       29 
29 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
       30 
30 
     | 
    
         
             
                requirements:
         
     | 
| 
       31 
     | 
    
         
            -
                - - " 
     | 
| 
      
 31 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
       32 
32 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       33 
     | 
    
         
            -
                    version: ' 
     | 
| 
      
 33 
     | 
    
         
            +
                    version: '10.4'
         
     | 
| 
       34 
34 
     | 
    
         
             
              type: :development
         
     | 
| 
       35 
35 
     | 
    
         
             
              prerelease: false
         
     | 
| 
       36 
36 
     | 
    
         
             
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       37 
37 
     | 
    
         
             
                requirements:
         
     | 
| 
       38 
     | 
    
         
            -
                - - " 
     | 
| 
      
 38 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
       39 
39 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       40 
     | 
    
         
            -
                    version: ' 
     | 
| 
      
 40 
     | 
    
         
            +
                    version: '10.4'
         
     | 
| 
       41 
41 
     | 
    
         
             
            description: only used to cipher integer, use in confused with ID
         
     | 
| 
       42 
42 
     | 
    
         
             
            email:
         
     | 
| 
       43 
43 
     | 
    
         
             
            - w.del@qq.com
         
     | 
| 
         @@ -52,7 +52,7 @@ files: 
     | 
|
| 
       52 
52 
     | 
    
         
             
            - Rakefile
         
     | 
| 
       53 
53 
     | 
    
         
             
            - id_cipher.gemspec
         
     | 
| 
       54 
54 
     | 
    
         
             
            - lib/id_cipher.rb
         
     | 
| 
       55 
     | 
    
         
            -
            -  
     | 
| 
      
 55 
     | 
    
         
            +
            - spec/id_cipher_spec.rb
         
     | 
| 
       56 
56 
     | 
    
         
             
            homepage: https://github.com/delongw/ruby-id-cipher
         
     | 
| 
       57 
57 
     | 
    
         
             
            licenses:
         
     | 
| 
       58 
58 
     | 
    
         
             
            - MIT
         
     | 
| 
         @@ -65,7 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       65 
65 
     | 
    
         
             
              requirements:
         
     | 
| 
       66 
66 
     | 
    
         
             
              - - ">="
         
     | 
| 
       67 
67 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       68 
     | 
    
         
            -
                  version: '0'
         
     | 
| 
      
 68 
     | 
    
         
            +
                  version: '2.0'
         
     | 
| 
       69 
69 
     | 
    
         
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
       70 
70 
     | 
    
         
             
              requirements:
         
     | 
| 
       71 
71 
     | 
    
         
             
              - - ">="
         
     | 
| 
         @@ -77,4 +77,5 @@ rubygems_version: 2.2.2 
     | 
|
| 
       77 
77 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       78 
78 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       79 
79 
     | 
    
         
             
            summary: cipher integer to another integer
         
     | 
| 
       80 
     | 
    
         
            -
            test_files: 
     | 
| 
      
 80 
     | 
    
         
            +
            test_files:
         
     | 
| 
      
 81 
     | 
    
         
            +
            - spec/id_cipher_spec.rb
         
     | 
    
        data/lib/id_cipher/version.rb
    DELETED