n_cipher 0.4.3 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6745670732254c4a64708cb93de4303e926b6387
4
- data.tar.gz: 1ef6c8752f76ed5899c6ecb88cdca30aa0a79096
3
+ metadata.gz: 085ee55c1f4188ae5c6cafc2e87a42ccb2e279a7
4
+ data.tar.gz: e7a842e0cbb49c4d5f068368d71143bee6c81697
5
5
  SHA512:
6
- metadata.gz: fa2eb75273ba70afaf3874d920a9e63eceb465f44519ee6ead3f459f6347ca4729508429e760f784ad5271c6f0638507927f1411543278fc99da400573a6d19d
7
- data.tar.gz: 519123fa2cc902e4d33db79f1e091bdc571a65eb1cac9b58101fa6382b6491efadc85435650aedde35ce8a0dcf9ca671670b37b37da398a0952ffebc1f927642
6
+ metadata.gz: 9c9b39423522f91c26e530f7f8f8a213441d89bc3746d93e2326299051284b22d7623c46127f4a75e68f176fae1508c7a75fe7db84877f3e12a5515b3065cea3
7
+ data.tar.gz: 8bc14fe168d59fa92adcdce3b667ee0e7faba59ebe4f61c314bd6415f5550702f3167654479399477265f975d5b1ada095fa491d254a454c50b97e02ddb93033
data/README.md CHANGED
@@ -5,8 +5,6 @@
5
5
  [![Travis branch](https://img.shields.io/travis/844196/n_cipher.svg)](https://travis-ci.org/844196/n_cipher)
6
6
  [![Coveralls branch](https://img.shields.io/coveralls/844196/n_cipher/master.svg)](https://coveralls.io/github/844196/n_cipher)
7
7
  [![Code Climate](https://img.shields.io/codeclimate/github/844196/n_cipher.svg)](https://codeclimate.com/github/844196/n_cipher)
8
- [![Docunment](https://img.shields.io/badge/docs-RubyDoc.info-blue.svg)](http://www.rubydoc.info/github/844196/n_cipher/master)
9
- [![Inline docs](http://inch-ci.org/github/844196/n_cipher.svg?branch=master&style=shields)](http://inch-ci.org/github/844196/n_cipher)
10
8
 
11
9
  ![](https://cloud.githubusercontent.com/assets/4990822/10408480/9bf5d63a-6f39-11e5-9568-55e24afcbdc5.png)
12
10
 
@@ -34,57 +32,15 @@ $ gem install n_cipher
34
32
 
35
33
  ## Usage
36
34
 
37
- ### Library
38
-
39
- ```ruby
40
- require 'n_cipher'
41
-
42
- NCipher.encode('にゃんぱす', seed: 'おうどん', delimiter: 'ひげ')
43
- #=> "んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ"
44
-
45
- NCipher.decode(
46
- 'んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ',
47
- seed: 'おうどん', delimiter: 'ひげ')
48
- #=> "にゃんぱす"
49
- ```
50
-
51
35
  ```ruby
52
- # いちいちオプション指定するのが面倒なとき用
53
36
  NCipher.configure do |config|
54
37
  config.seed = 'おうどん'
55
38
  config.delimiter = 'ひげ'
56
39
  end
57
40
 
58
41
  NCipher.encode 'にゃんぱす'
59
- #=> "んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ"
42
+ #=> "んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどう"
60
43
 
61
- NCipher.decode 'んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ'
44
+ NCipher.decode 'んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどう'
62
45
  #=> "にゃんぱす"
63
46
  ```
64
-
65
- ### Command
66
-
67
- シード値と区切り文字は以下の環境変数でも設定できます
68
-
69
- - `NCIPHER_SEED` : シード値
70
- - `NCIPHER_DELIMITER` : 区切り文字
71
-
72
- ```shellsession
73
- $ # encode
74
- $ n_cipher encode 'にゃんぱす'
75
- ぱすすにすに〜ぱすすゃぱす〜ぱすすんんに〜ぱすすゃにゃ〜ぱすすににん〜
76
- $ n_cipher encode --seed 'おうどん' --delimiter 'ひげ' 'にゃんぱす'
77
- んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ
78
- ```
79
-
80
- ```shellsession
81
- $ # decode
82
- $ n_cipher decode --seed 'おうどん' --delimiter 'ひげ' 'んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ'
83
- にゃんぱす
84
- ```
85
-
86
- ```shellsession
87
- $ # support STDIN
88
- $ renge | n_cipher encode
89
- んんぱんにぱに〜ゃぱんぱゃゃん〜ぱすすにぱゃ〜ぱすすゃにぱ〜ぱすすゃすゃ〜すににゃぱに〜ゃんゃんぱゃぱ〜ゃぱゃんんぱぱ〜ぱすすににに〜ぱすすにゃぱ〜ぱすすんんに〜ぱすすにぱす〜すにすんんんゃ〜
90
- ```
@@ -0,0 +1,83 @@
1
+ module NCipher::ArgumentValidation
2
+ @@validations = {}
3
+
4
+ def self.included(klass)
5
+ klass.extend(ClassMethod)
6
+ klass.extend(InheritValidation)
7
+ Method.send(:include, MonkeyPatchToMethodClass)
8
+ end
9
+
10
+ module MonkeyPatchToMethodClass
11
+ def validation
12
+ ::NCipher::ArgumentValidation.validations[owner]["__#{name}__"]
13
+ end
14
+ end
15
+
16
+ module InheritValidation
17
+ def inherited(klass)
18
+ super
19
+ # 継承先へバリデーションをディープコピー
20
+ baseclass_validations = ::NCipher::ArgumentValidation.validations[self]
21
+ ::NCipher::ArgumentValidation.validations[klass] = baseclass_validations.map {|k, v| [k, v.map(&:clone)] }.to_h
22
+ end
23
+ end
24
+
25
+ module ClassMethod
26
+ private
27
+
28
+ def args_validation(method, message=nil, &validation)
29
+ exception = ArgumentError.new(message).tap do |ex|
30
+ # バックトレースの偽装
31
+ ex.set_backtrace(validation.source_location.push("in `args_validation'").join(':'))
32
+ end
33
+
34
+ ::NCipher::ArgumentValidation.store_validation(self, "__#{method}__", validation, exception)
35
+
36
+ unless private_method_defined?("__#{method}__")
37
+ ::NCipher::ArgumentValidation.around_alias(self, "__#{method}__", method)
38
+ ::NCipher::ArgumentValidation.define_proxy_method(self, method)
39
+ end
40
+
41
+ self
42
+ end
43
+ end
44
+
45
+ class << self
46
+ def store_validation(owner, method_name, validation, exception)
47
+ @@validations[owner] ||= {}
48
+ @@validations[owner][method_name] ||= []
49
+ @@validations[owner][method_name] << {
50
+ :validation => validation,
51
+ :exception => exception
52
+ }
53
+ end
54
+
55
+ def define_proxy_method(owner, name)
56
+ owner.class_eval do
57
+ define_method(name) do |*args, &block|
58
+ validations = ::NCipher::ArgumentValidation.validations.tap do |hash|
59
+ break hash.has_key?(self.class) ? hash[self.class] : hash[self.singleton_class]
60
+ end
61
+
62
+ validations["__#{name}__"].each do |pattern|
63
+ # ブロックをインスタンスのコンテキストで評価
64
+ raise(pattern[:exception]) unless self.instance_exec(*args, block, &pattern[:validation])
65
+ end
66
+
67
+ __send__("__#{name}__", *args, &block)
68
+ end
69
+ end
70
+ end
71
+
72
+ def around_alias(owner, new_name, old_name)
73
+ owner.class_eval do
74
+ private(old_name)
75
+ alias_method new_name, old_name
76
+ end
77
+ end
78
+
79
+ def validations
80
+ @@validations
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,45 @@
1
+ class NCipher::Configuration
2
+ include NCipher::ArgumentValidation
3
+
4
+ attr_reader :seed
5
+ attr_accessor :delimiter
6
+
7
+ def initialize
8
+ reset
9
+ end
10
+
11
+ def reset
12
+ @seed = 'にゃんぱす'.chars
13
+ @delimiter = '〜'
14
+ end
15
+
16
+ def seed=(string)
17
+ @seed = string.chars
18
+ end
19
+
20
+ args_validation :seed=, 'Seed must be 2 to 36 characters.' do |seed|
21
+ seed.length.between?(2, 36)
22
+ end
23
+
24
+ args_validation :seed=, 'Character is duplicated in seed.' do |seed|
25
+ seed.length == seed.chars.uniq.length
26
+ end
27
+
28
+ args_validation :seed=, 'Seed and delimiter are duplicated.' do |seed|
29
+ !seed.include?(@delimiter)
30
+ end
31
+
32
+ args_validation :delimiter=, 'Delimiter and seed are duplicated.' do |delimiter|
33
+ !@seed.join.include?(delimiter)
34
+ end
35
+ end
36
+
37
+ class << NCipher
38
+ def config
39
+ @config ||= NCipher::Configuration.new
40
+ end
41
+
42
+ def configure(&block)
43
+ block.call(config)
44
+ end
45
+ end
@@ -1,4 +1,4 @@
1
1
  module NCipher
2
2
  # バージョン番号
3
- VERSION = '0.4.3'
3
+ VERSION = '0.5.0'
4
4
  end
data/lib/n_cipher.rb CHANGED
@@ -1,154 +1,53 @@
1
+ module NCipher; end
1
2
  require 'n_cipher/version'
2
-
3
- # ユニコードエスケープシーケンスを用いた簡易的な暗号化及び復号化方式を提供するモジュール
4
- module NCipher
5
- # デフォルト値
6
- @seed = 'にゃんぱす'
7
- @delimiter = '〜'
8
-
9
- # シード値、区切り文字を設定するためのモジュール
10
- module Configuration
11
- # @!attribute seed
12
- # シード値
13
- # @!attribute delimiter
14
- # 区切り文字
15
- attr_accessor :seed, :delimiter
16
-
17
- # @example
18
- # NCipher.configure do |config|
19
- # config.seed = 'abc'
20
- # config.delimiter = ','
21
- # end
22
- def configure
23
- yield self
24
- end
3
+ require 'n_cipher/argument_validation'
4
+ require 'n_cipher/configuration'
5
+
6
+ class << NCipher
7
+ include NCipher::ArgumentValidation
8
+
9
+ def encode(string)
10
+ string
11
+ .to_str
12
+ .unpack('U*')
13
+ .map {|char| char.to_s(NCipher.config.seed.length) }
14
+ .map {|char| char.gsub(/./, convert_table(:for => :encode)) }
15
+ .join(NCipher.config.delimiter)
25
16
  end
26
17
 
27
- # 実際の暗号化、復号化を行うメソッドが定義されているモジュール
28
- # @note このモジュール内のメソッドはプライベートクラスメソッドに指定されているため、外部から呼び出すことはできない
29
- module Convert
30
- # シード値から変換テーブルを構築する
31
- #
32
- # @example
33
- # convert_table(:encode, 'あいうえお')
34
- # #=> {"0"=>"あ", "1"=>"い", "2"=>"う", "3"=>"え", "4"=>"お"}
35
- #
36
- # convert_table(:decode, 'あいうえお')
37
- # #=> {"あ"=>"0", "い"=>"1", "う"=>"2", "え"=>"3", "お"=>"4"}
38
- #
39
- # @param [Symbol] mode +:encode+もしくは+:decode+を指定
40
- # @param [String] seed 元となるシード値
41
- #
42
- # @return [Hash] 変換テーブル
43
- #
44
- # @raise [RangeError] シード値が2文字以下、もしくは36文字以上の場合
45
- def convert_table(mode, seed)
46
- fail RangeError, 'Seed must be 2 to 36 characters.' unless seed.size.between?(2, 36)
47
-
48
- table = [*'0'..'9', *'a'..'z'].zip(seed.chars).reject(&:one?).to_h
49
- case mode
50
- when :encode then table
51
- when :decode then table.invert
52
- end
53
- end
54
-
55
- # 引数チェック
56
- #
57
- # @param [Symbol] mode +:encode+もしくは+:decode+を指定
58
- # @param [String] string 対象文字列
59
- # @param [String] seed シード値
60
- # @param [String] delimiter 区切り文字
61
- #
62
- # @return [true] チェックをパスした場合
63
- #
64
- # @raise [ArgumentError]
65
- def validate_arguments(mode, string, seed, delimiter)
66
- fail ArgumentError, 'Seed and delimiter are duplicated.' unless (seed.chars & delimiter.chars).size.zero?
67
- fail ArgumentError, 'Character is duplicated in seed.' unless seed.size == seed.chars.uniq.size
68
-
69
- if mode == :decode
70
- fail ArgumentError, 'Delimiter is not include in the cipher string.' unless string.match(delimiter)
71
- fail ArgumentError, 'Invalid cipher string.' unless (string.chars - "#{seed}#{delimiter}".chars).size.zero?
72
- end
73
-
74
- true
75
- end
18
+ def decode(string)
19
+ string
20
+ .to_str
21
+ .split(NCipher.config.delimiter)
22
+ .map {|char| char.gsub(/./, convert_table(:for => :decode)) }
23
+ .map {|char| char.to_i(NCipher.config.seed.length) }
24
+ .map {|char| [char].pack('U') }
25
+ .join
26
+ end
76
27
 
77
- # 実際の変換処理を行う
78
- #
79
- # @param [Symbol] mode +:encode+もしくは+:decode+を指定
80
- # @param [String] string 対象文字列
81
- # @param [String] seed シード値
82
- # @param [String] delimiter 区切り文字
83
- #
84
- # @return [String] 変換された文字列オブジェクト
85
- def convert(mode, string, seed, delimiter)
86
- validate_arguments(mode, string, seed, delimiter)
28
+ args_validation :encode, "Arguments must be respond to 'to_str' method." do |string|
29
+ string.respond_to? :to_str
30
+ end
87
31
 
88
- table = convert_table(mode.to_sym, seed)
89
- rtn = case mode
90
- when :encode
91
- string.unpack('U*').map {|ele| ele.to_s(seed.size).gsub(/./, table).concat(delimiter) }
92
- when :decode
93
- string.split(delimiter).map {|ele| [ele.gsub(/./, table).to_i(seed.size)].pack('U') }
94
- end
32
+ args_validation :decode, "Arguments must be respond to 'to_str' method." do |string|
33
+ string.respond_to? :to_str
34
+ end
95
35
 
96
- rtn.join
97
- end
36
+ args_validation :decode, 'Delimiter is not include in the cipher string.' do |string|
37
+ string.to_str.match(NCipher.config.delimiter)
98
38
  end
99
39
 
100
- class << self
101
- include NCipher::Configuration
102
- include NCipher::Convert
40
+ args_validation :decode, 'Invalid cipher string.' do |string|
41
+ (string.to_str.chars - "#{NCipher.config.seed.join}#{NCipher.config.delimiter}".chars).size.zero?
42
+ end
103
43
 
104
- # 文字列を暗号化
105
- #
106
- # @note このメソッドは{Convert#convert}のラッパーメソッドである
107
- #
108
- # @example
109
- # NCipher.encode('abc', seed: 'にゃんぱす', delimiter: '〜')
110
- # #=> "ぱすん〜ぱすぱ〜ぱすす〜"
111
- #
112
- # @param [#to_str] string 対象文字列
113
- # @param [#to_str] seed シード値
114
- # @param [#to_str] delimiter 区切り文字
115
- #
116
- # @return [String] 暗号化された文字列オブジェクト
117
- #
118
- # @raise [TypeError]
119
- #
120
- # @see Convert#convert
121
- def encode(string, seed: @seed, delimiter: @delimiter)
122
- [string, seed, delimiter].each do |obj|
123
- fail TypeError, "Arguments must be respond to 'to_str' method." unless obj.respond_to? :to_str
124
- end
125
- convert(:encode, string.to_str, seed.to_str, delimiter.to_str)
126
- end
44
+ private
127
45
 
128
- # 文字列を復号化
129
- #
130
- # @note このメソッドは{Convert#convert}のラッパーメソッドである
131
- #
132
- # @example
133
- # NCipher.decode('ぱすん〜ぱすぱ〜ぱすす〜', seed: 'にゃんぱす', delimiter: '〜')
134
- # #=> "abc"
135
- #
136
- # @param [#to_str] string 対象文字列
137
- # @param [#to_str] seed シード値
138
- # @param [#to_str] delimiter 区切り文字
139
- #
140
- # @return [String] 復号化された文字列オブジェクト
141
- #
142
- # @raise [TypeError]
143
- #
144
- # @see Convert#convert
145
- def decode(string, seed: @seed, delimiter: @delimiter)
146
- [string, seed, delimiter].each do |obj|
147
- fail TypeError, "Arguments must be respond to 'to_str' method." unless obj.respond_to? :to_str
148
- end
149
- convert(:decode, string.to_str, seed.to_str, delimiter.to_str)
46
+ def convert_table(param={:for => nil})
47
+ table = [*'0'..'9', *'a'..'z'].zip(NCipher.config.seed).reject(&:one?).to_h
48
+ case param[:for]
49
+ when :encode then table
50
+ when :decode then table.invert
150
51
  end
151
52
  end
152
-
153
- private_class_method :convert_table, :convert
154
53
  end
data/n_cipher.gemspec CHANGED
@@ -20,12 +20,10 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ['lib']
21
21
 
22
22
  spec.required_ruby_version = '>= 2.2.0'
23
- spec.add_development_dependency 'bundler', '~> 1.9'
24
- spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'rake'
25
25
  spec.add_development_dependency 'rspec'
26
26
  spec.add_development_dependency 'coveralls'
27
27
  spec.add_development_dependency 'guard'
28
28
  spec.add_development_dependency 'guard-rspec'
29
-
30
- spec.add_dependency 'thor'
31
29
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: n_cipher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaya Takeda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-26 00:00:00.000000000 Z
11
+ date: 2016-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.9'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.9'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
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: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -94,25 +94,10 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: thor
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
97
  description: 文字列のUnicodeエスケープシーケンスを利用した簡易的な暗号です
112
98
  email:
113
99
  - 844196@gmail.com
114
- executables:
115
- - n_cipher
100
+ executables: []
116
101
  extensions: []
117
102
  extra_rdoc_files: []
118
103
  files:
@@ -126,8 +111,9 @@ files:
126
111
  - Rakefile
127
112
  - bin/console
128
113
  - bin/setup
129
- - exe/n_cipher
130
114
  - lib/n_cipher.rb
115
+ - lib/n_cipher/argument_validation.rb
116
+ - lib/n_cipher/configuration.rb
131
117
  - lib/n_cipher/version.rb
132
118
  - n_cipher.gemspec
133
119
  homepage: https://github.com/844196/n_cipher
data/exe/n_cipher DELETED
@@ -1,36 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'n_cipher'
4
- require 'thor'
5
-
6
- class NCipherCLI < Thor
7
- map %w(-v --version) => :version
8
- class_option :seed, :type => :string
9
- class_option :delimiter, :type => :string
10
-
11
- sub_commands = {'encode' => ['encode <STRING>', '文字列をN暗号化'],
12
- 'decode' => ['decode <STRING>', 'N暗号文字列を復号化']}
13
-
14
- sub_commands.each_pair do |command, (synopsis, description)|
15
- desc(synopsis, description)
16
- define_method(command) do |string = nil|
17
- begin
18
- string ||= $stdin.read.chomp
19
- seed = options[:seed] || ENV['NCIPHER_SEED'] || 'にゃんぱす'
20
- delimiter = options[:delimiter] || ENV['NCIPHER_DELIMITER'] || '〜'
21
- STDOUT.puts NCipher.send(command, string, seed: seed, delimiter: delimiter)
22
- rescue => e
23
- STDERR.puts "#{__FILE__}: #{e.message}"
24
- exit 1
25
- end
26
- end
27
- end
28
-
29
- desc 'version', 'Print version'
30
- def version
31
- STDOUT.puts NCipher::VERSION
32
- exit 0
33
- end
34
- end
35
-
36
- NCipherCLI.start