This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
The MIT License
|
||||
|
||||
Copyright (C) 2010 Max Prokopiev, Alexandar Simic, Caige Nichols
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,50 @@
|
||||
# RC4
|
||||
|
||||
RC4 is a pure Ruby implementation of the Rc4 algorithm.
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
First require the gem:
|
||||
|
||||
require 'rc4'
|
||||
|
||||
To encrypt:
|
||||
|
||||
key = "nuff rspec"
|
||||
enc = RC4.new(key)
|
||||
encrypted = enc.encrypt("super-cool-test")
|
||||
|
||||
To decrypt:
|
||||
|
||||
dec = RC4.new(key)
|
||||
decrypted = dec.decrypt(encrypted)
|
||||
|
||||
Since encrypt method is used for encryption and decryption, decrypt is
|
||||
just an alias to encrypt in order to make the usage more intuitive.
|
||||
|
||||
# Note
|
||||
|
||||
The original algorithm implementation in Ruby by Max Prokopiev
|
||||
<max-prokopiev@yandex.ru>.
|
||||
|
||||
Aleksandar Simic then modified it to include a test suite and gem
|
||||
packaged it using gem-this.
|
||||
<asimic@gmail.com>
|
||||
|
||||
|
||||
I switched the project to use rspec2 and am the now the project's primary maintainer.
|
||||
|
||||
Caige Nichols <caigesn@gmail.com>
|
||||
|
||||
# License
|
||||
|
||||
Ruby-RC4 is released under the MIT license.
|
||||
|
||||
# Contributors
|
||||
|
||||
(Please let me know if I missed anyone)
|
||||
|
||||
- [caiges](http://github.com/caiges)
|
||||
- [juggler](http://github.com/juggler) (original author)
|
||||
- Alexandar Simic
|
||||
@@ -0,0 +1,115 @@
|
||||
require "rubygems"
|
||||
require "rake/gempackagetask"
|
||||
require "rake/rdoctask"
|
||||
require "rspec"
|
||||
require "rspec/core/rake_task"
|
||||
|
||||
RSpec::Core::RakeTask.new do |t|
|
||||
t.rspec_opts = %w(--format nested --colour)
|
||||
t.pattern = './spec'
|
||||
end
|
||||
|
||||
|
||||
task :default => ["spec"]
|
||||
|
||||
# This builds the actual gem. For details of what all these options
|
||||
# mean, and other ones you can add, check the documentation here:
|
||||
#
|
||||
# http://rubygems.org/read/chapter/20
|
||||
#
|
||||
spec = Gem::Specification.new do |s|
|
||||
|
||||
# Change these as appropriate
|
||||
s.name = "ruby-rc4"
|
||||
s.version = "0.1.5"
|
||||
s.summary = "RubyRC4 is a pure Ruby implementation of the RC4 algorithm."
|
||||
s.author = "Caige Nichols"
|
||||
s.email = "caigesn@gmail.com"
|
||||
s.homepage = "http://www.caigenichols.com/"
|
||||
|
||||
s.has_rdoc = false
|
||||
s.extra_rdoc_files = %w(README.md)
|
||||
s.rdoc_options = %w(--main README.md)
|
||||
|
||||
# Add any extra files to include in the gem
|
||||
s.files = %w(LICENSE README.md Rakefile) + Dir.glob("{spec,lib/**/*}")
|
||||
s.require_paths = ["lib"]
|
||||
|
||||
# If you want to depend on other gems, add them here, along with any
|
||||
# relevant versions
|
||||
# s.add_dependency("some_other_gem", "~> 0.1.0")
|
||||
|
||||
# If your tests use any gems, include them here
|
||||
s.add_development_dependency("rspec")
|
||||
|
||||
# If you want to publish automatically to rubyforge, you'll may need
|
||||
# to tweak this, and the publishing task below too.
|
||||
s.rubyforge_project = "ruby-rc4"
|
||||
end
|
||||
|
||||
# This task actually builds the gem. We also regenerate a static
|
||||
# .gemspec file, which is useful if something (i.e. GitHub) will
|
||||
# be automatically building a gem for this project. If you're not
|
||||
# using GitHub, edit as appropriate.
|
||||
Rake::GemPackageTask.new(spec) do |pkg|
|
||||
pkg.gem_spec = spec
|
||||
|
||||
# Generate the gemspec file for github.
|
||||
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
||||
File.open(file, "w") {|f| f << spec.to_ruby }
|
||||
end
|
||||
|
||||
# Generate documentation
|
||||
Rake::RDocTask.new do |rd|
|
||||
rd.main = "README"
|
||||
rd.rdoc_files.include("README", "lib/**/*.rb")
|
||||
rd.rdoc_dir = "rdoc"
|
||||
end
|
||||
|
||||
desc 'Clear out RDoc and generated packages'
|
||||
task :clean => [:clobber_rdoc, :clobber_package] do
|
||||
rm "#{spec.name}.gemspec"
|
||||
end
|
||||
|
||||
# If you want to publish to RubyForge automatically, here's a simple
|
||||
# task to help do that. If you don't, just get rid of this.
|
||||
# Be sure to set up your Rubyforge account details with the Rubyforge
|
||||
# gem; you'll need to run `rubyforge setup` and `rubyforge config` at
|
||||
# the very least.
|
||||
begin
|
||||
require "rake/contrib/sshpublisher"
|
||||
namespace :rubyforge do
|
||||
|
||||
desc "Release gem and RDoc documentation to RubyForge"
|
||||
task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
|
||||
|
||||
namespace :release do
|
||||
desc "Release a new version of this gem"
|
||||
task :gem => [:package] do
|
||||
require 'rubyforge'
|
||||
rubyforge = RubyForge.new
|
||||
rubyforge.configure
|
||||
rubyforge.login
|
||||
rubyforge.userconfig['release_notes'] = spec.summary
|
||||
path_to_gem = File.join(File.dirname(__FILE__), "pkg", "#{spec.name}-#{spec.version}.gem")
|
||||
puts "Publishing #{spec.name}-#{spec.version.to_s} to Rubyforge..."
|
||||
rubyforge.add_release(spec.rubyforge_project, spec.name, spec.version.to_s, path_to_gem)
|
||||
end
|
||||
|
||||
desc "Publish RDoc to RubyForge."
|
||||
task :docs => [:rdoc] do
|
||||
config = YAML.load(
|
||||
File.read(File.expand_path('~/.rubyforge/user-config.yml'))
|
||||
)
|
||||
|
||||
host = "#{config['username']}@rubyforge.org"
|
||||
remote_dir = "/var/www/gforge-projects/ruby-rc4/" # Should be the same as the rubyforge project name
|
||||
local_dir = 'rdoc'
|
||||
|
||||
Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
|
||||
end
|
||||
end
|
||||
end
|
||||
rescue LoadError
|
||||
puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
|
||||
end
|
||||
@@ -0,0 +1,43 @@
|
||||
class RC4
|
||||
|
||||
def initialize(str)
|
||||
begin
|
||||
raise SyntaxError, "RC4: Key supplied is blank" if str.eql?('')
|
||||
|
||||
@q1, @q2 = 0, 0
|
||||
@key = []
|
||||
str.each_byte {|elem| @key << elem} while @key.size < 256
|
||||
@key.slice!(256..@key.size-1) if @key.size >= 256
|
||||
@s = (0..255).to_a
|
||||
j = 0
|
||||
0.upto(255) do |i|
|
||||
j = (j + @s[i] + @key[i] )%256
|
||||
@s[i], @s[j] = @s[j], @s[i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def encrypt!(text)
|
||||
process text
|
||||
end
|
||||
|
||||
def encrypt(text)
|
||||
process text.dup
|
||||
end
|
||||
|
||||
alias_method :decrypt, :encrypt
|
||||
|
||||
private
|
||||
|
||||
def process(text)
|
||||
text.unpack("C*").map { |c| c ^ round }.pack("C*")
|
||||
end
|
||||
|
||||
def round
|
||||
@q1 = (@q1 + 1)%256
|
||||
@q2 = (@q2 + @s[@q1])%256
|
||||
@s[@q1], @s[@q2] = @s[@q2], @s[@q1]
|
||||
@s[(@s[@q1]+@s[@q2])%256]
|
||||
end
|
||||
|
||||
end
|
||||
Executable
+60
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env ruby
|
||||
# coding: ascii
|
||||
|
||||
require 'rc4'
|
||||
require 'rspec'
|
||||
|
||||
describe RC4 do
|
||||
it "should not crypt with a blank key" do
|
||||
expect {
|
||||
RC4.new("")
|
||||
}.to raise_error(SyntaxError, "RC4: Key supplied is blank")
|
||||
|
||||
end
|
||||
|
||||
it "should encrypt and decrypt password 'super-cool-test' with key 'nuff rspec'" do
|
||||
key = "nuff rspec"
|
||||
enc = RC4.new(key)
|
||||
encrypted = enc.encrypt("super-cool-test")
|
||||
|
||||
dec = RC4.new(key)
|
||||
decrypted = dec.decrypt(encrypted)
|
||||
decrypted.should match(/super-cool-test/)
|
||||
end
|
||||
|
||||
it "should encrypt and decrypt password 'if-I-was-a-bit' with key 'bitsnbytes'" do
|
||||
enc = RC4.new('bitsnbytes')
|
||||
dec = RC4.new('bitsnbytes')
|
||||
|
||||
encrypted = enc.encrypt("if-I-was-a-bit")
|
||||
decrypted = dec.decrypt(encrypted)
|
||||
decrypted.should match(/if-I-was-a-bit/)
|
||||
end
|
||||
|
||||
|
||||
# test samples taken from:
|
||||
# http://en.wikipedia.org/wiki/RC4#Test_vectors
|
||||
|
||||
it "should decrypt ciphertext 'BBF316E8D940AF0AD3' with key 'Key' to 'Plaintext'" do
|
||||
dec = RC4.new('Key')
|
||||
|
||||
decrypted = dec.decrypt(['BBF316E8D940AF0AD3'].pack("H*"))
|
||||
decrypted.should match(/Plaintext/)
|
||||
end
|
||||
|
||||
it "should decrypt ciphertext '1021BF0420' with key 'Wiki' to 'pedia' " do
|
||||
dec = RC4.new('Wiki')
|
||||
|
||||
decrypted = dec.decrypt(['1021BF0420'].pack("H*"))
|
||||
decrypted.should match(/pedia/)
|
||||
end
|
||||
|
||||
it "should decrypt ciphertext '45A01F645FC35B383552544B9BF5' with key 'Secret' to 'Attack at dawn'" do
|
||||
dec = RC4.new('Secret')
|
||||
|
||||
decrypted = dec.decrypt(['45A01F645FC35B383552544B9BF5'].pack("H*"))
|
||||
decrypted.should match(/Attack at dawn/)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user