ruby-rc4 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +39 -0
- data/Rakefile +115 -0
- data/lib/rc4.rb +44 -0
- metadata +83 -0
data/README
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
== RC4
|
2
|
+
|
3
|
+
RC4 is a pure Ruby implementation of the Rc4 algorithm.
|
4
|
+
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
First require the gem:
|
9
|
+
|
10
|
+
require 'rc4'
|
11
|
+
|
12
|
+
To encrypt:
|
13
|
+
|
14
|
+
key = "nuff rspec"
|
15
|
+
enc = RC4.new(key)
|
16
|
+
encrypted = enc.encrypt("super-cool-test")
|
17
|
+
|
18
|
+
To decrypt:
|
19
|
+
|
20
|
+
dec = RC4.new(key)
|
21
|
+
decrypted = dec.decrypt(encrypted)
|
22
|
+
|
23
|
+
Since encrypt method is used for encryption and decryption, decrypt is
|
24
|
+
just an alias to encrypt in order to make the usage more intuitive.
|
25
|
+
|
26
|
+
== Note
|
27
|
+
|
28
|
+
The original algorithm implementation in Ruby by Max Prokopiev
|
29
|
+
<max-prokopiev@yandex.ru>.
|
30
|
+
|
31
|
+
Aleksandar Simic then modified it to include a test suite and gem
|
32
|
+
packaged it using gem-this.
|
33
|
+
<asimic@gmail.com>
|
34
|
+
|
35
|
+
|
36
|
+
I switched the project to use rspec2.
|
37
|
+
|
38
|
+
Thanks,
|
39
|
+
Caige Nichols <caigesn@gmail.com>
|
data/Rakefile
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake/gempackagetask"
|
3
|
+
require "rake/rdoctask"
|
4
|
+
require "rspec"
|
5
|
+
require "rspec/core/rake_task"
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new do |t|
|
8
|
+
t.rspec_opts = %w(--format nested --colour)
|
9
|
+
t.pattern = './spec'
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
task :default => ["spec"]
|
14
|
+
|
15
|
+
# This builds the actual gem. For details of what all these options
|
16
|
+
# mean, and other ones you can add, check the documentation here:
|
17
|
+
#
|
18
|
+
# http://rubygems.org/read/chapter/20
|
19
|
+
#
|
20
|
+
spec = Gem::Specification.new do |s|
|
21
|
+
|
22
|
+
# Change these as appropriate
|
23
|
+
s.name = "ruby-rc4"
|
24
|
+
s.version = "0.1.1"
|
25
|
+
s.summary = "RubyRC4 is a pure Ruby implementation of the RC4 algorithm."
|
26
|
+
s.author = "Caige Nichols"
|
27
|
+
s.email = "caigesn@gmail.com"
|
28
|
+
s.homepage = "http://www.caigenichols.com/"
|
29
|
+
|
30
|
+
s.has_rdoc = false
|
31
|
+
s.extra_rdoc_files = %w(README)
|
32
|
+
s.rdoc_options = %w(--main README)
|
33
|
+
|
34
|
+
# Add any extra files to include in the gem
|
35
|
+
s.files = %w(README Rakefile) + Dir.glob("{spec,lib/**/*}")
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
|
38
|
+
# If you want to depend on other gems, add them here, along with any
|
39
|
+
# relevant versions
|
40
|
+
# s.add_dependency("some_other_gem", "~> 0.1.0")
|
41
|
+
|
42
|
+
# If your tests use any gems, include them here
|
43
|
+
s.add_development_dependency("rspec")
|
44
|
+
|
45
|
+
# If you want to publish automatically to rubyforge, you'll may need
|
46
|
+
# to tweak this, and the publishing task below too.
|
47
|
+
s.rubyforge_project = "ruby-rc4"
|
48
|
+
end
|
49
|
+
|
50
|
+
# This task actually builds the gem. We also regenerate a static
|
51
|
+
# .gemspec file, which is useful if something (i.e. GitHub) will
|
52
|
+
# be automatically building a gem for this project. If you're not
|
53
|
+
# using GitHub, edit as appropriate.
|
54
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
55
|
+
pkg.gem_spec = spec
|
56
|
+
|
57
|
+
# Generate the gemspec file for github.
|
58
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
59
|
+
File.open(file, "w") {|f| f << spec.to_ruby }
|
60
|
+
end
|
61
|
+
|
62
|
+
# Generate documentation
|
63
|
+
Rake::RDocTask.new do |rd|
|
64
|
+
rd.main = "README"
|
65
|
+
rd.rdoc_files.include("README", "lib/**/*.rb")
|
66
|
+
rd.rdoc_dir = "rdoc"
|
67
|
+
end
|
68
|
+
|
69
|
+
desc 'Clear out RDoc and generated packages'
|
70
|
+
task :clean => [:clobber_rdoc, :clobber_package] do
|
71
|
+
rm "#{spec.name}.gemspec"
|
72
|
+
end
|
73
|
+
|
74
|
+
# If you want to publish to RubyForge automatically, here's a simple
|
75
|
+
# task to help do that. If you don't, just get rid of this.
|
76
|
+
# Be sure to set up your Rubyforge account details with the Rubyforge
|
77
|
+
# gem; you'll need to run `rubyforge setup` and `rubyforge config` at
|
78
|
+
# the very least.
|
79
|
+
begin
|
80
|
+
require "rake/contrib/sshpublisher"
|
81
|
+
namespace :rubyforge do
|
82
|
+
|
83
|
+
desc "Release gem and RDoc documentation to RubyForge"
|
84
|
+
task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
|
85
|
+
|
86
|
+
namespace :release do
|
87
|
+
desc "Release a new version of this gem"
|
88
|
+
task :gem => [:package] do
|
89
|
+
require 'rubyforge'
|
90
|
+
rubyforge = RubyForge.new
|
91
|
+
rubyforge.configure
|
92
|
+
rubyforge.login
|
93
|
+
rubyforge.userconfig['release_notes'] = spec.summary
|
94
|
+
path_to_gem = File.join(File.dirname(__FILE__), "pkg", "#{spec.name}-#{spec.version}.gem")
|
95
|
+
puts "Publishing #{spec.name}-#{spec.version.to_s} to Rubyforge..."
|
96
|
+
rubyforge.add_release(spec.rubyforge_project, spec.name, spec.version.to_s, path_to_gem)
|
97
|
+
end
|
98
|
+
|
99
|
+
desc "Publish RDoc to RubyForge."
|
100
|
+
task :docs => [:rdoc] do
|
101
|
+
config = YAML.load(
|
102
|
+
File.read(File.expand_path('~/.rubyforge/user-config.yml'))
|
103
|
+
)
|
104
|
+
|
105
|
+
host = "#{config['username']}@rubyforge.org"
|
106
|
+
remote_dir = "/var/www/gforge-projects/ruby-rc4/" # Should be the same as the rubyforge project name
|
107
|
+
local_dir = 'rdoc'
|
108
|
+
|
109
|
+
Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
rescue LoadError
|
114
|
+
puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
|
115
|
+
end
|
data/lib/rc4.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
class RC4
|
2
|
+
|
3
|
+
def initialize(str)
|
4
|
+
begin
|
5
|
+
raise SyntaxError, "RC4: Key supplied is blank" if str.eql?('')
|
6
|
+
|
7
|
+
@q1, @q2 = 0, 0
|
8
|
+
@key = []
|
9
|
+
str.each_byte {|elem| @key << elem} while @key.size < 256
|
10
|
+
@key.slice!(256..@key.size-1) if @key.size >= 256
|
11
|
+
@s = (0..255).to_a
|
12
|
+
j = 0
|
13
|
+
0.upto(255) do |i|
|
14
|
+
j = (j + @s[i] + @key[i] )%256
|
15
|
+
@s[i], @s[j] = @s[j], @s[i]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def encrypt!(text)
|
21
|
+
process text
|
22
|
+
end
|
23
|
+
|
24
|
+
def encrypt(text)
|
25
|
+
process text.dup
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :decrypt, :encrypt
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def process(text)
|
33
|
+
0.upto(text.length-1) {|i| text[i] = text[i] ^ round}
|
34
|
+
text
|
35
|
+
end
|
36
|
+
|
37
|
+
def round
|
38
|
+
@q1 = (@q1 + 1)%256
|
39
|
+
@q2 = (@q2 + @s[@q1])%256
|
40
|
+
@s[@q1], @s[@q2] = @s[@q2], @s[@q1]
|
41
|
+
@s[(@s[@q1]+@s[@q2])%256]
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-rc4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Caige Nichols
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-25 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description:
|
36
|
+
email: caigesn@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
files:
|
44
|
+
- README
|
45
|
+
- Rakefile
|
46
|
+
- lib/rc4.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://www.caigenichols.com/
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --main
|
54
|
+
- README
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project: ruby-rc4
|
78
|
+
rubygems_version: 1.5.0
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: RubyRC4 is a pure Ruby implementation of the RC4 algorithm.
|
82
|
+
test_files: []
|
83
|
+
|