myrb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ec976fba9b87b38bad791b8296dbc8899fbd044
4
+ data.tar.gz: ad294c44d86b00396a49494d6ad19bb8e1a32ee9
5
+ SHA512:
6
+ metadata.gz: 94d298435418cae4537fbfc8b82d66585279f6024ff10f94784b7843ad769ddb5e6134dd79322e8a049096740a724978381df1367b29340e5809b3d25cf3de47
7
+ data.tar.gz: d43fcb0f090c8d53a90826d7913f6e52b8737a0880786d3b58dde409f0cca42c61cfa9e12896ea9fd021f86af1320370eda07214741ed4b319a5aa9c52e1f5a8
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.2
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in my.gemspec
4
+ gemspec
5
+
6
+ gem 'sshkey'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 zhangyijin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ # my.rb
2
+
3
+ A code snippets collection maybe usefull.
4
+
5
+ ## Installation
6
+
7
+ Gemfile
8
+
9
+ ```ruby
10
+ gem 'myrb', require: 'my'
11
+ ```
12
+
13
+
14
+ gem install
15
+
16
+ $ gem install myrb
17
+
18
+ ## License
19
+
20
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
21
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "my"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+
2
+ # My::Combination 用例:破解SSH-KEY 的密码
3
+
4
+ # gem install sshkey
5
+ require "sshkey"
6
+ require "my/combination"
7
+
8
+ # 由于性能问题,最好只用于7位数以下、只包含数字和字母的密码
9
+ # chars = [*'A'..'Z', *'a'..'z', *'0'..'9']
10
+ chars = [*'0'..'9']
11
+
12
+ # 密码位数
13
+ digit = 6
14
+
15
+ # 生成随机密码
16
+ password = digit.times.map{ chars.sample }.join
17
+
18
+ filename = "/tmp/my_ssh_key_brute_forc"
19
+
20
+ # 生成带密码的 ssh key
21
+ system "ssh-keygen -N #{password} -f #{filename}"
22
+
23
+ key = File.read(filename)
24
+
25
+ # TODO 可以利用 start 和 stop 分发到多个进程中执行
26
+ comb = My::Combination.new([chars]*digit)
27
+ start = Time.now
28
+
29
+ puts "Start at #{start}"
30
+
31
+ loop do
32
+ begin
33
+ pw = comb.values.join
34
+ SSHKey.new(key, passphrase: pw)
35
+ puts "Index is #{comb.index}, Password is #{pw}, Used #{Time.now - start}"
36
+ break
37
+ rescue OpenSSL::PKey::DSAError
38
+ end
39
+ comb.succ!
40
+ end
@@ -0,0 +1,5 @@
1
+ require "my/version"
2
+
3
+ module My
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,34 @@
1
+
2
+ module My
3
+ # 使用 ruby 字符串实现的 bitmap,并不比 set 快多少,但是很节约内存(数字比较小的情况)
4
+ class Bitmap
5
+ attr_reader :count, :max_value
6
+
7
+ def initialize(max_value)
8
+ @max_value = max_value
9
+ @bitmap = "\0".b * (max_value/8 + 1)
10
+ @count = 0
11
+ end
12
+
13
+ def <<(value)
14
+ raise IndexError, 'out of range' if value > max_value || value < 1
15
+ bytes, bits = value.divmod(8)
16
+ if bits > 0
17
+ mask = 1 << (8 - bits)
18
+ else
19
+ bytes -= 1
20
+ mask = 1
21
+ end
22
+ int = @bitmap[bytes].ord
23
+ return value if int & mask > 0 # 已经存在
24
+ @count += 1
25
+ @bitmap[bytes] = (int | mask).chr
26
+ value
27
+ end
28
+
29
+ def to_s
30
+ @bitmap.chars.map{|c| c.ord.to_s(2).rjust(8, '0') }.join[0...max_value]
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+
2
+
3
+ module My
4
+ class Combination
5
+
6
+ attr_reader :index, :length
7
+
8
+ # 范围包含 start,不包含 stop
9
+ def initialize arrays, start: 0, stop: nil
10
+ @arrays = arrays
11
+ @length = arrays.reduce(1){|n, a| a.size * n }
12
+ @start = start
13
+ @index = start
14
+ @stop = stop || length
15
+ @stop = length if @stop > length
16
+ raise IndexError if @stop <= @start
17
+ end
18
+
19
+ def values
20
+ i = @index
21
+ @arrays.map do |array|
22
+ i, n = i.divmod(array.size)
23
+ array[n]
24
+ end
25
+ end
26
+
27
+ def succ!
28
+ raise StopIteration unless succ?
29
+ @index += 1
30
+ nil
31
+ end
32
+
33
+ def succ?
34
+ @index < @stop - 1
35
+ end
36
+
37
+ def diff_count
38
+ @index - @start
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+
2
+ module My
3
+
4
+ class Limit
5
+
6
+ def initialize limit
7
+ @check_point = limit / 10.0 # 每执行 N 次后进行一次检查,默认为次数限制的 1/10
8
+ @max_sleep_ms = 100 # 每次暂停的毫秒数
9
+ @count = 0
10
+ end
11
+
12
+ def incr n
13
+ @prev_time ||= Time.now
14
+ @count += n
15
+ if @count >= @check_point
16
+ current_time = Time.now
17
+ sleep_time = @max_sleep_ms - (current_time - @prev_time)*1000
18
+ if sleep_time > 0
19
+ sleep sleep_time/1000.0
20
+ @prev_time = Time.now
21
+ else
22
+ @prev_time = current_time
23
+ end
24
+ @count = 0
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+
2
+ module My
3
+ class ReadLine
4
+ include Enumerable
5
+
6
+ def initialize input = nil, &block
7
+ @input = input || Enumerator.new(&block)
8
+ end
9
+
10
+ def each
11
+ last_line = ''
12
+ @input.each do |chunk|
13
+ chunk.each_line do |line|
14
+ if line[-1] == "\n"
15
+ unless last_line.empty?
16
+ last_line << line
17
+ line, last_line = last_line, ''
18
+ end
19
+ yield line
20
+ else
21
+ last_line << line
22
+ end
23
+ end
24
+ end
25
+ yield last_line unless last_line.empty?
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+
2
+ # 分割连续的子序列
3
+ module My
4
+ class SplitSequence
5
+
6
+ def split(seq)
7
+ result = []
8
+ return result if seq.length == 0
9
+ group = new_group
10
+ group << seq[0]
11
+ 1.upto(seq.length - 1) do |i|
12
+ unless succ?(group[group.length - 1], seq[i])
13
+ result << group
14
+ group = new_group
15
+ end
16
+ group << seq[i]
17
+ end
18
+ result << group
19
+ result
20
+ end
21
+
22
+ # 创建新的分组
23
+ def new_group
24
+ raise NotImplementedError
25
+ end
26
+
27
+ # 判断两个成员是否连续
28
+ def succ?(a, b)
29
+ raise NotImplementedError
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+
2
+ module My
3
+
4
+ module Unicode
5
+
6
+ module_function
7
+
8
+ # decode('\u4EA6\u53EF\u8D5B\u8247')
9
+ # => '亦可赛艇'
10
+ def decode(str)
11
+ str.gsub(/\\u([0-9a-fA-F]{4})/){ [$1.to_i(16)].pack('U') }
12
+ end
13
+
14
+ # decode_bytes('\xE9\x92\xA6\xE5\xAE\x9A')
15
+ # => '钦定'
16
+ def decode_bytes(str)
17
+ str.gsub(/\\x([0-9a-fA-F]{2})/){ $1.to_i(16).chr }.force_encoding("utf-8")
18
+ end
19
+
20
+ # encode('亦可赛艇')
21
+ # => ‘\u4EA6\u53EF\u8D5B\u8247’
22
+ def encode(str)
23
+ str.each_codepoint.reduce(''){|s, cp| s << '\\u%04X' % cp }
24
+ end
25
+
26
+ # encode_bytes('钦定')
27
+ # => '\xE9\x92\xA6\xE5\xAE\x9A'
28
+ def encode_bytes(str)
29
+ str.each_byte.reduce(''){|s, b| s << '\x%02X' % b }
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,3 @@
1
+ module My
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'my/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "myrb"
8
+ spec.version = My::VERSION
9
+ spec.authors = ["John King"]
10
+ spec.email = ["myrb_gem@john1king.com"]
11
+
12
+ spec.summary = %q{A code snippets collection maybe usefull.}
13
+ spec.description = %q{A code snippets collection maybe usefull.}
14
+ spec.homepage = "https://github.com/john1king/my.rb"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.required_ruby_version = ">= 2.0"
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: myrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John King
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: A code snippets collection maybe usefull.
56
+ email:
57
+ - myrb_gem@john1king.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - examples/ssh_key.rb
71
+ - lib/my.rb
72
+ - lib/my/bitmap.rb
73
+ - lib/my/combination.rb
74
+ - lib/my/limit.rb
75
+ - lib/my/read_line.rb
76
+ - lib/my/split_sequence.rb
77
+ - lib/my/unicode.rb
78
+ - lib/my/version.rb
79
+ - my.gemspec
80
+ homepage: https://github.com/john1king/my.rb
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '2.0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.2
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: A code snippets collection maybe usefull.
104
+ test_files: []