ircblowfish 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dea6b0abfbbd1e7c640e33889753fa44469b04ee
4
+ data.tar.gz: 180f2968dbbb59c2c70e03db62f4c729c7b45c5a
5
+ SHA512:
6
+ metadata.gz: 4917e35b598efff54cb5e3b855165c4d87e4901b49a08449ef7a1a139c1864f90d18c16d87e432c54bf2f1beb6be46f4bcc7d39e378bab617f7d81a8981f8f45
7
+ data.tar.gz: fb23a33b987751d9c1516e2f0802254ab096e7fa0def0a859b5febca819239689ced1e504d68bcd45d5d5fa35ca09da76a4ba36c6870c02e3ae5ffe30a51d86e
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --warnings
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0 (2016-04-29)
2
+
3
+ Initial Release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ircblowfish.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jason Iverson
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.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # IrcBlowfish
2
+
3
+ A Ruby module for encrypting and decrypting IRC Blowfish ECB/CBC messages.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ircblowfish'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ircblowfish
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ message = '+OK '
25
+ key = 'ecb:AWeakKey'
26
+ plaintext = IrcBlowfish.decrypt message, key
27
+
28
+ text = 'This is a test string'
29
+ key = 'cbc:ABetter?Key'
30
+ message = IrcBlowfish.encrypt text, key
31
+ ```
32
+
33
+ To define an ECB key, prefix it with either `ecb:` or `old:`. CBC keys can be prefixed with `cbc:` or are assumed to be CBC with no prefix.
34
+
35
+ You can explicitly call `encrypt_ecb` or `encrypt_cbc`, but just calling `encrypt` will automatically figure out which to use based on the key passed.
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JasonIverson/ircblowfish.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
51
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ircblowfish"
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
data/bin/setup ADDED
@@ -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,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ircblowfish/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ircblowfish"
8
+ spec.version = IrcBlowfish::VERSION
9
+ spec.authors = ["Jason Iverson"]
10
+ spec.email = ["iverson.jason.code@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby Encryption Module for IRC Blowfish Messages}
13
+ spec.description = %q{A Ruby module for encrypting and decrypting IRC Blowfish ECB/CBC messages}
14
+ spec.homepage = "https://github.com/JasonIverson/ircblowfish-ruby/"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,139 @@
1
+ require "ircblowfish/version"
2
+ require "ircblowfish/base64"
3
+
4
+ require "base64"
5
+ require "openssl"
6
+ require "securerandom"
7
+
8
+ module IrcBlowfish
9
+ def self.encrypt(msg, key)
10
+ return encrypt_ecb(msg, key) if key =~ /^(?:old|ecb):/
11
+ return encrypt_cbc(msg, key)
12
+ end
13
+
14
+ def self.decrypt(msg, key)
15
+ return decrypt_ecb(msg, key) if key =~ /^(?:old|ecb):/
16
+ return decrypt_cbc(msg, key)
17
+ end
18
+
19
+ def self.encrypt_ecb(msg, key)
20
+ return msg if key.nil? or key == ''
21
+ return msg if msg.nil? or msg == ''
22
+
23
+ # Force encoding to binary in case of non-ascii messages
24
+ plaintext = msg.dup.force_encoding 'BINARY'
25
+
26
+ # Remove the ecb:/old: prefix if it's used
27
+ key = key.sub %r{^(?:old|ecb):}, ''
28
+ return msg if key == ''
29
+
30
+ # Create the Blowfish-CBC cipher
31
+ cipher = OpenSSL::Cipher.new 'bf-ecb'
32
+ cipher.encrypt
33
+ cipher.key_len = key.length
34
+ cipher.key = key
35
+ cipher.padding = 0
36
+
37
+ # Add null padding to make the length a multiple of 8
38
+ plaintext += "\x00" * (8 - (plaintext.bytesize % 8))
39
+
40
+ # Generate the IRC message with prefix
41
+ '+OK ' + IrcBlowfish::Base64.encode(cipher.update(plaintext))
42
+ end
43
+
44
+ def self.decrypt_ecb(msg, key)
45
+ return msg if key.nil? or key == ''
46
+ return msg if msg.nil? or msg == ''
47
+
48
+ # Ensure the message is a valid Blowfish-ECB message and remove the prefix
49
+ ciphertext = msg.dup
50
+ return msg unless ciphertext.sub! %r{^\+OK }, ''
51
+ return msg if ciphertext[0] == '*' # Dump if this is actually a Blowfish-CBC message
52
+ return '' if ciphertext == '' # I've seen some clients send "+OK " for null messages
53
+
54
+ # Remove the ecb:/old: prefix if it's used
55
+ key = key.sub %r{^(?:old|ecb):}, ''
56
+ return msg if key == ''
57
+
58
+ # Create the Blowfish-CBC cipher
59
+ cipher = OpenSSL::Cipher.new 'bf-ecb'
60
+ cipher.decrypt
61
+ cipher.key_len = key.length
62
+ cipher.key = key
63
+ cipher.padding = 0
64
+
65
+ # Decrypt the ciphertext and remove the trailing padding
66
+ cipher.update(IrcBlowfish::Base64.decode(ciphertext)).sub! %r{\x00*$}, ''
67
+ end
68
+
69
+ def self.encrypt_cbc(msg, key)
70
+ return msg if key.nil? or key == ''
71
+ return msg if msg.nil? or msg == ''
72
+
73
+ # Force encoding to binary in case of non-ascii messages
74
+ plaintext = msg.dup.force_encoding 'BINARY'
75
+
76
+ # Remove the cbc: prefix if it's used
77
+ key = key.sub %r{^cbc:}, ''
78
+ return msg if key == ''
79
+
80
+ # Generate a random IV of length 8
81
+ iv = random_iv 8
82
+
83
+ # Create the Blowfish-CBC cipher
84
+ cipher = OpenSSL::Cipher.new 'bf-cbc'
85
+ cipher.encrypt
86
+ cipher.key_len = key.length
87
+ cipher.key = key
88
+ cipher.padding = 0
89
+ cipher.iv = iv
90
+
91
+ # Add null padding to make the length a multiple of 8
92
+ plaintext += "\x00" * (8 - (plaintext.bytesize % 8))
93
+
94
+ # Generate the IRC message with prefix
95
+ '+OK *' + ::Base64.encode64(iv + cipher.update(plaintext)).gsub!(/\n/, '')
96
+ end
97
+
98
+ def self.decrypt_cbc(msg, key)
99
+ return msg if key.nil? or key == ''
100
+ return msg if msg.nil? or msg == ''
101
+
102
+ # Ensure the message is a valid Blowfish-CBC message and remove the prefix
103
+ ciphertext = msg.dup
104
+ return msg unless ciphertext.sub! %r{^\+OK \*}, ''
105
+
106
+ # Remove the cbc: prefix if it's used
107
+ key = key.sub %r{^cbc:}, ''
108
+ return msg if key == ''
109
+
110
+ # Decode the text to get the IV + ciphertext
111
+ ciphertext = ::Base64.decode64 ciphertext
112
+
113
+ return msg if ciphertext.bytesize < 8
114
+
115
+ iv = ciphertext[0,8] # Extract the IV from the string
116
+ ciphertext = ciphertext[8..-1] # Remove the IV from the string
117
+
118
+ return '' if ciphertext == ''
119
+
120
+ # Create the Blowfish-CBC cipher
121
+ cipher = OpenSSL::Cipher.new 'bf-cbc'
122
+ cipher.decrypt
123
+ cipher.key_len = key.length
124
+ cipher.key = key
125
+ cipher.padding = 0
126
+ cipher.iv = iv
127
+
128
+ # Decrypt the ciphertext and remove the trailing padding
129
+ cipher.update(ciphertext).sub! %r{\x00*$}, ''
130
+ end
131
+
132
+ private
133
+
134
+ def self.random_iv(len)
135
+ # Valid characters for IRC Blowfish-CBC IV: a-z A-Z 0-9 _
136
+ letters = [('a'..'z'),('A'..'Z'),('0'..'9')].map { |i| i.to_a }.flatten << '_'
137
+ (0...len).map { letters[SecureRandom.random_number(letters.length)] }.join
138
+ end
139
+ end
@@ -0,0 +1,48 @@
1
+ module IrcBlowfish
2
+ module Base64
3
+ B64 = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.freeze
4
+
5
+ def self.encode(bin)
6
+ str = ''
7
+
8
+ k = -1
9
+ while k < bin.length - 1
10
+ (l,r) = [0,0].map do |i|
11
+ [24, 16, 8, 0].each do |j|
12
+ i += bin[k+=1].ord << j
13
+ end
14
+ i
15
+ end
16
+ [r,l].each do |i|
17
+ 6.times do
18
+ str += B64[i & 0x3F]
19
+ i >>= 6
20
+ end
21
+ end
22
+ end
23
+
24
+ str
25
+ end
26
+
27
+ def self.decode(str)
28
+ bin = ''
29
+
30
+ k = -1
31
+ while k < str.length - 1
32
+ (l,r) = [0,0].map do |i|
33
+ 6.times do |j|
34
+ i |= B64.index(str[k+=1]) << (j * 6)
35
+ end
36
+ i
37
+ end
38
+ [r,l].each do |i|
39
+ [24, 16, 8, 0].each do |j|
40
+ bin += ((i & (0xFF << j)) >> j).chr
41
+ end
42
+ end
43
+ end
44
+
45
+ bin
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module IrcBlowfish
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ircblowfish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Iverson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-01 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: A Ruby module for encrypting and decrypting IRC Blowfish ECB/CBC messages
56
+ email:
57
+ - iverson.jason.code@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CHANGELOG.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - ircblowfish.gemspec
73
+ - lib/ircblowfish.rb
74
+ - lib/ircblowfish/base64.rb
75
+ - lib/ircblowfish/version.rb
76
+ homepage: https://github.com/JasonIverson/ircblowfish-ruby/
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Ruby Encryption Module for IRC Blowfish Messages
100
+ test_files: []