ffi-rc4 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d4b9fe3b1016358c8bb9dc7bd8ee7c1d177ae14a
4
+ data.tar.gz: f9c4055fb2240b8938453febe8dcec4db157fde3
5
+ SHA512:
6
+ metadata.gz: fa83ef4698077220c40db5bc4c7fcc3c7770505a7abdf8bb051708865780b222daf217c31f2214bb750b909656b177c70737a44e1cc06360075c872c14f8bb35
7
+ data.tar.gz: a522e1be79451610ea84245dc866d0376b0cf849d0a950ae869f7932ca5e88beb5fe4adcff7604cd9c84189fff767ccf8f7dd9404305707565ef58ba93530932
@@ -0,0 +1,10 @@
1
+ .ruby-*
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -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.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ffi-rc4.gemspec
4
+ gemspec
@@ -0,0 +1,49 @@
1
+ # FFI::RC4
2
+
3
+ A simple FFI wrapper around openssl RC4 for unsafe RC4 encryption, **do not use for production systems if you expect anything to be considered "secure", use a better algorithm like AES-256-CBC**
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ffi-rc4'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ffi-rc4
20
+
21
+ ## Usage
22
+ ```ruby
23
+ require 'ffi-rc4'
24
+ FFI_RC4.default = "secret" # setup a default secret if desired, each call takes a secret as well
25
+
26
+ # get back base64 encoded bytes
27
+ encrypted_base64 = FFI_RC4::encrypt_base64("hello")
28
+ text = FFI_RC4::decrypt_base64(encrypt_base64)
29
+
30
+ # or get back bytes
31
+ encrypted = FFI_RC4::encrypt("hello")
32
+ text = FFI_RC4::decrypt(encrypted)
33
+
34
+ # or send secret with call
35
+ encrypted_base64 = FFI_RC4::encrypt_base64("hello", "other_secret")
36
+ text = FFI_RC4::decrypt_base64(encrypted_base64, "other_secret")
37
+
38
+ ```
39
+
40
+ ## Development
41
+
42
+ 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.
43
+
44
+ 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).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ffi-rc4.
49
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ffi-rc4"
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,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ffi-rc4/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ffi-rc4"
8
+ spec.version = FFI_RC4::VERSION
9
+ spec.authors = ["Brandon Dewitt"]
10
+ spec.email = ["brandonsdewitt+rubygems@gmail.com"]
11
+
12
+ spec.summary = %q{ OpenSSL/RC4 through FFI }
13
+ spec.description = %q{ OpenSSL/RC4 through FFI }
14
+ spec.homepage = "https://github.com/abrandoned"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "ffi"
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.12"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
@@ -0,0 +1,74 @@
1
+ require "ffi"
2
+ require "ffi-rc4/version"
3
+ require "base64"
4
+ require "digest"
5
+
6
+ module FFI_RC4
7
+ extend FFI::Library
8
+ if FFI::Platform.windows?
9
+ ffi_lib 'libeay32', 'ssleay32'
10
+ else
11
+ ffi_lib 'ssl'
12
+ end
13
+
14
+ class RC4_KEY < FFI::Struct
15
+ layout :x => :uint,
16
+ :y => :uint,
17
+ :data => [:uint, 256]
18
+ end
19
+
20
+ attach_function :RC4_set_key, [:pointer, :int, :pointer], :void, :blocking => true
21
+ attach_function :RC4, [:pointer, :int, :pointer, :pointer], :void, :blocking => true
22
+
23
+ def self.default
24
+ @default || nil
25
+ end
26
+
27
+ def self.default=(value)
28
+ @default = value
29
+ end
30
+
31
+ def self.decrypt(encrypted_bytes, secret = ::FFI_RC4.default)
32
+ raise ArgumentError, "secret cannot be nil" if secret.nil?
33
+
34
+ secret_digest = "#{::Digest::SHA256.hexdigest(secret)}#{::Digest::SHA256.hexdigest(secret.reverse)}"
35
+ rc4_key = RC4_KEY.new
36
+ rc4_key[:data].to_ptr.put_string(0, secret_digest)
37
+
38
+ in_data = ::FFI::MemoryPointer.from_string(encrypted_bytes)
39
+ out_data = ::FFI::MemoryPointer.new(:char, encrypted_bytes.length)
40
+ secret_pointer = ::FFI::MemoryPointer.from_string(secret_digest)
41
+ RC4_set_key(rc4_key.pointer, secret_digest.length, secret_pointer)
42
+ RC4(rc4_key.pointer, encrypted_bytes.length, in_data, out_data)
43
+
44
+ out_data.read_string
45
+ end
46
+
47
+ def self.decrypt_base64(encrypted_base64, secret = ::FFI_RC4.default)
48
+ decrypt(::Base64::urlsafe_decode64(encrypted_base64), secret)
49
+ end
50
+
51
+ def self.encrypt(text, secret = ::FFI_RC4.default)
52
+ raise ArgumentError, "secret cannot be nil" if secret.nil?
53
+
54
+ secret_digest = "#{::Digest::SHA256.hexdigest(secret)}#{::Digest::SHA256.hexdigest(secret.reverse)}"
55
+ rc4_key = RC4_KEY.new
56
+ rc4_key[:data].to_ptr.put_string(0, secret_digest)
57
+
58
+ in_data = ::FFI::MemoryPointer.from_string(text)
59
+ out_data = ::FFI::MemoryPointer.new(:char, text.length)
60
+ secret_pointer = ::FFI::MemoryPointer.from_string(secret_digest)
61
+ RC4_set_key(rc4_key.pointer, secret_digest.length, secret_pointer)
62
+ RC4(rc4_key.pointer, text.length, in_data, out_data)
63
+
64
+ out_data.read_string
65
+ end
66
+
67
+ def self.encrypt_and_base64(text, secret = ::FFI_RC4.default)
68
+ ::Base64::urlsafe_encode64(encrypt(text, secret))
69
+ end
70
+
71
+ class << self
72
+ alias_method :encrypt_base64, :encrypt_and_base64
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module FFI_RC4
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-rc4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Dewitt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: " OpenSSL/RC4 through FFI "
70
+ email:
71
+ - brandonsdewitt+rubygems@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - ffi-rc4.gemspec
85
+ - lib/ffi-rc4.rb
86
+ - lib/ffi-rc4/version.rb
87
+ homepage: https://github.com/abrandoned
88
+ licenses: []
89
+ metadata:
90
+ allowed_push_host: https://rubygems.org
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.5.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: OpenSSL/RC4 through FFI
111
+ test_files: []