vx_crypt 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +14 -0
- data/Rakefile +3 -0
- data/lib/vx_crypt/version.rb +3 -0
- data/lib/vx_crypt.rb +97 -0
- data/vx_crypt.gemspec +21 -0
- metadata +69 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/Rakefile
ADDED
data/lib/vx_crypt.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
class VxCrypt
|
5
|
+
|
6
|
+
def initialize(key = "", base64 = true)
|
7
|
+
@hash_key = _hash(key).to_s
|
8
|
+
@hash_length = @hash_key.to_s.length()
|
9
|
+
@base64 = base64
|
10
|
+
@salt = Digest::SHA1.hexdigest("#{rand(Integer::MAX)}")
|
11
|
+
end
|
12
|
+
|
13
|
+
def encrypt(str)
|
14
|
+
iv = _generate_iv()
|
15
|
+
out = ""
|
16
|
+
c = 0
|
17
|
+
|
18
|
+
while c < @hash_length
|
19
|
+
out += (iv[c] ^ @hash_key[c]).chr
|
20
|
+
c += 1
|
21
|
+
end
|
22
|
+
|
23
|
+
key = iv
|
24
|
+
c = 0
|
25
|
+
|
26
|
+
while c < str.length()
|
27
|
+
if c != 0 and (c % @hash_length == 0)
|
28
|
+
key = _hash(key + str[c - @hash_length, @hash_length])
|
29
|
+
end
|
30
|
+
|
31
|
+
out += (key[c % @hash_length].ord ^ str[c].ord).chr
|
32
|
+
c += 1
|
33
|
+
end
|
34
|
+
if @base64
|
35
|
+
out = Base64.encode64(out).gsub("\n", "")
|
36
|
+
end
|
37
|
+
out
|
38
|
+
end
|
39
|
+
|
40
|
+
def decrypt(str)
|
41
|
+
if @base64
|
42
|
+
str = Base64.decode64(str)
|
43
|
+
end
|
44
|
+
|
45
|
+
tmp_iv = str[0, @hash_length]
|
46
|
+
str = str[@hash_length, str.length()]
|
47
|
+
iv = ""
|
48
|
+
out = ""
|
49
|
+
c = 0
|
50
|
+
while c < @hash_length
|
51
|
+
iv += (tmp_iv[c] ^ @hash_key[c]).chr
|
52
|
+
c += 1
|
53
|
+
end
|
54
|
+
|
55
|
+
key = iv
|
56
|
+
c = 0
|
57
|
+
while c < str.length
|
58
|
+
if (c != 0 and (c % @hash_length == 0))
|
59
|
+
key = _hash(key . out[c - @hash_length, @hash_length])
|
60
|
+
end
|
61
|
+
|
62
|
+
out += (key[c % @hash_length] ^ str[c]).chr
|
63
|
+
c += 1
|
64
|
+
end
|
65
|
+
|
66
|
+
out
|
67
|
+
end
|
68
|
+
|
69
|
+
def _generate_iv()
|
70
|
+
_hash(@salt)
|
71
|
+
end
|
72
|
+
|
73
|
+
def _hash(key)
|
74
|
+
sha1 = Digest::SHA1.hexdigest(key)
|
75
|
+
|
76
|
+
out = ""
|
77
|
+
c = 0
|
78
|
+
while c < sha1.length()
|
79
|
+
out += _hex2chr(sha1[c].chr + sha1[c+1].chr)
|
80
|
+
c += 2
|
81
|
+
end
|
82
|
+
|
83
|
+
out
|
84
|
+
end
|
85
|
+
|
86
|
+
def _hex2chr(num)
|
87
|
+
num.hex.chr
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
class Integer
|
93
|
+
N_BYTES = [42].pack('i').size
|
94
|
+
N_BITS = N_BYTES * 8
|
95
|
+
MAX = 2 ** (N_BITS - 2) - 1
|
96
|
+
MIN = -MAX - 1
|
97
|
+
end
|
data/vx_crypt.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "vx_crypt/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "vx_crypt"
|
7
|
+
s.version = VxCrypt::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["VEXXHOST"]
|
10
|
+
s.email = ["dev@vexxhost.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Encryption mechanism that uses salting and hashing}
|
13
|
+
s.description = %q{Encryption mechanism that uses salting and hashing}
|
14
|
+
|
15
|
+
s.rubyforge_project = "vx_crypt"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vx_crypt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- VEXXHOST
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-04-18 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Encryption mechanism that uses salting and hashing
|
22
|
+
email:
|
23
|
+
- dev@vexxhost.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Gemfile.lock
|
34
|
+
- Rakefile
|
35
|
+
- lib/vx_crypt.rb
|
36
|
+
- lib/vx_crypt/version.rb
|
37
|
+
- vx_crypt.gemspec
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: ""
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project: vx_crypt
|
64
|
+
rubygems_version: 1.3.6
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Encryption mechanism that uses salting and hashing
|
68
|
+
test_files: []
|
69
|
+
|