n_cipher 0.4.1 → 0.4.2
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/.rubocop.yml +2 -0
- data/Guardfile +2 -2
- data/README.md +26 -16
- data/exe/n_cipher +5 -3
- data/lib/n_cipher/version.rb +1 -1
- data/lib/n_cipher.rb +27 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2126680aa66698771c9b79645302925e95b1cf2b
|
4
|
+
data.tar.gz: 798adc75827207896f8f49ce53846fc66b1c29b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9cf3ba5c3326214ba61743de75ca1676c0d58fdebdd2585c1555daf6bebb6370d8e88e78472746d77ab2368deda8da82c19aab39e0b0d3ddf91035c08e5852c
|
7
|
+
data.tar.gz: f47651c4242b453a998e06c66f775b9b75bbb5353aae3ee0fc9e7baddf2dd9258f6025b58c52a2cfe78e5001db3c84eaf123648323cb3e795f1815ca2ea325a3
|
data/.rubocop.yml
CHANGED
data/Guardfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# vim:set filetype=ruby:
|
2
2
|
|
3
3
|
guard :shell do
|
4
|
-
watch(
|
5
|
-
watch(
|
4
|
+
watch(%r{(?:lib|test)/.+\.rb}) { `bundle exec rake test` }
|
5
|
+
watch(%r{lib/.+\.rb}) { `bundle exec rake yard` }
|
6
6
|
end
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
[](https://travis-ci.org/844196/n_cipher)
|
6
6
|
[](https://coveralls.io/github/844196/n_cipher)
|
7
7
|
[](https://codeclimate.com/github/844196/n_cipher)
|
8
|
-
[](http://www.rubydoc.info/github/844196/n_cipher/master)
|
9
9
|
[](http://inch-ci.org/github/844196/n_cipher)
|
10
10
|
|
11
11
|

|
@@ -39,41 +39,51 @@ $ gem install n_cipher
|
|
39
39
|
```ruby
|
40
40
|
require 'n_cipher'
|
41
41
|
|
42
|
-
NCipher
|
42
|
+
NCipher.encode('にゃんぱす', seed: 'おうどん', delimiter: 'ひげ')
|
43
43
|
#=> "んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ"
|
44
44
|
|
45
|
-
NCipher
|
45
|
+
NCipher.decode(
|
46
46
|
'んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ',
|
47
47
|
seed: 'おうどん', delimiter: 'ひげ')
|
48
48
|
#=> "にゃんぱす"
|
49
49
|
```
|
50
50
|
|
51
|
+
```ruby
|
52
|
+
# いちいちオプション指定するのが面倒なとき用
|
53
|
+
NCipher.configure do |config|
|
54
|
+
config.seed = 'おうどん'
|
55
|
+
config.delimiter = 'ひげ'
|
56
|
+
end
|
57
|
+
|
58
|
+
NCipher.encode 'にゃんぱす'
|
59
|
+
#=> "んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ"
|
60
|
+
|
61
|
+
NCipher.decode 'んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ'
|
62
|
+
#=> "にゃんぱす"
|
63
|
+
```
|
64
|
+
|
51
65
|
### Command
|
52
66
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
n_cipher encode <STRING> # 文字列をN暗号化
|
58
|
-
n_cipher help [COMMAND] # Describe available commands or one specific command
|
59
|
-
n_cipher version # Print version
|
60
|
-
|
61
|
-
Options:
|
62
|
-
[--seed=SEED]
|
63
|
-
# Default: にゃんぱす
|
64
|
-
[--delimiter=DELIMITER]
|
65
|
-
# Default: 〜
|
67
|
+
シード値と区切り文字は以下の環境変数でも設定できます
|
68
|
+
|
69
|
+
- `NCIPHER_SEED` : シード値
|
70
|
+
- `NCIPHER_DELIMITER` : 区切り文字
|
66
71
|
|
72
|
+
```shellsession
|
67
73
|
$ # encode
|
68
74
|
$ n_cipher encode 'にゃんぱす'
|
69
75
|
ぱすすにすに〜ぱすすゃぱす〜ぱすすんんに〜ぱすすゃにゃ〜ぱすすににん〜
|
70
76
|
$ n_cipher encode --seed 'おうどん' --delimiter 'ひげ' 'にゃんぱす'
|
71
77
|
んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ
|
78
|
+
```
|
72
79
|
|
80
|
+
```
|
73
81
|
$ # decode
|
74
82
|
$ n_cipher decode --seed 'おうどん' --delimiter 'ひげ' 'んおおうどどんひげんおおどおおんひげんおおどうおんひげんおおうんおうひげんおおううどうひげ'
|
75
83
|
にゃんぱす
|
84
|
+
```
|
76
85
|
|
86
|
+
```
|
77
87
|
$ # support STDIN
|
78
88
|
$ renge | n_cipher encode
|
79
89
|
んんぱんにぱに〜ゃぱんぱゃゃん〜ぱすすにぱゃ〜ぱすすゃにぱ〜ぱすすゃすゃ〜すににゃぱに〜ゃんゃんぱゃぱ〜ゃぱゃんんぱぱ〜ぱすすににに〜ぱすすにゃぱ〜ぱすすんんに〜ぱすすにぱす〜すにすんんんゃ〜
|
data/exe/n_cipher
CHANGED
@@ -5,8 +5,8 @@ require 'thor'
|
|
5
5
|
|
6
6
|
class NCipherCLI < Thor
|
7
7
|
map %w(-v --version) => :version
|
8
|
-
class_option :seed, :type => :string
|
9
|
-
class_option :delimiter, :type => :string
|
8
|
+
class_option :seed, :type => :string
|
9
|
+
class_option :delimiter, :type => :string
|
10
10
|
|
11
11
|
sub_commands = {'encode' => ['encode <STRING>', '文字列をN暗号化'],
|
12
12
|
'decode' => ['decode <STRING>', 'N暗号文字列を復号化']}
|
@@ -16,7 +16,9 @@ class NCipherCLI < Thor
|
|
16
16
|
define_method(command) do |string = nil|
|
17
17
|
begin
|
18
18
|
string ||= $stdin.read.chomp
|
19
|
-
|
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)
|
20
22
|
rescue => e
|
21
23
|
STDERR.puts "#{__FILE__}: #{e.message}"
|
22
24
|
exit 1
|
data/lib/n_cipher/version.rb
CHANGED
data/lib/n_cipher.rb
CHANGED
@@ -52,29 +52,46 @@ module NCipher
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
#
|
55
|
+
# 引数チェック
|
56
56
|
#
|
57
57
|
# @param [Symbol] mode +:encode+もしくは+:decode+を指定
|
58
58
|
# @param [String] string 対象文字列
|
59
59
|
# @param [String] seed シード値
|
60
60
|
# @param [String] delimiter 区切り文字
|
61
61
|
#
|
62
|
-
# @return [
|
62
|
+
# @return [true] チェックをパスした場合
|
63
63
|
#
|
64
64
|
# @raise [ArgumentError]
|
65
|
-
def
|
65
|
+
def validate_arguments(mode, string, seed, delimiter)
|
66
66
|
fail ArgumentError, 'Seed and delimiter are duplicated.' unless (seed.chars & delimiter.chars).size.zero?
|
67
67
|
fail ArgumentError, 'Character is duplicated in seed.' unless seed.size == seed.chars.uniq.size
|
68
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
|
76
|
+
|
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)
|
87
|
+
|
69
88
|
table = convert_table(mode.to_sym, seed)
|
70
89
|
rtn = case mode
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
string.split(delimiter).map {|ele| [ele.gsub(/./, table).to_i(seed.size)].pack('U') }
|
77
|
-
end
|
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
|
78
95
|
|
79
96
|
rtn.join
|
80
97
|
end
|
@@ -98,9 +115,7 @@ module NCipher
|
|
98
115
|
#
|
99
116
|
# @return [String] 暗号化された文字列オブジェクト
|
100
117
|
#
|
101
|
-
# @raise [ArgumentError]
|
102
118
|
# @raise [TypeError]
|
103
|
-
# @raise [RangeError]
|
104
119
|
#
|
105
120
|
# @see Convert#convert
|
106
121
|
def encode(string, seed: @seed, delimiter: @delimiter)
|
@@ -124,9 +139,7 @@ module NCipher
|
|
124
139
|
#
|
125
140
|
# @return [String] 復号化された文字列オブジェクト
|
126
141
|
#
|
127
|
-
# @raise [ArgumentError]
|
128
142
|
# @raise [TypeError]
|
129
|
-
# @raise [RangeError]
|
130
143
|
#
|
131
144
|
# @see Convert#convert
|
132
145
|
def decode(string, seed: @seed, delimiter: @delimiter)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: n_cipher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masaya Takeda
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|