digest_perfect 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/README.md +9 -0
- data/Rakefile +2 -0
- data/digest_perfect.gemspec +21 -0
- data/lib/digest_perfect.rb +78 -0
- data/lib/digest_perfect/version.rb +3 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
Usage
|
2
|
+
=====
|
3
|
+
|
4
|
+
Digest::Perfect.hexdigest("String to digest")
|
5
|
+
|
6
|
+
Credits
|
7
|
+
=======
|
8
|
+
|
9
|
+
Most of the code in this gem was taken from the excellend PageRankr gem ( https://github.com/blatyo/page_rankr ) by Allen Madsen. I just made a few tweaks to make the code more general-purpose.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "digest_perfect/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "digest_perfect"
|
7
|
+
s.version = DigestPerfect::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Starr Horne"]
|
10
|
+
s.email = ["starr@chromahq.com"]
|
11
|
+
s.homepage = "https://github.com/starrhorne/digest_perfect"
|
12
|
+
s.summary = %q{Implementation of Bob Jenkins' perfect hash algorithm in ruby}
|
13
|
+
s.description = %q{Among other things, this is the hash used to create requests to google's pagerank tool. Most of the code comes from the excellent PageRankr gem.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "digest_perfect"
|
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
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require "digest"
|
2
|
+
|
3
|
+
module Digest
|
4
|
+
|
5
|
+
class Perfect
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def hexdigest(string_to_hash)
|
10
|
+
bytes = byte_array(string_to_hash)
|
11
|
+
length = bytes.length
|
12
|
+
a = b = 0x9E3779B9
|
13
|
+
c = 0xE6359A60
|
14
|
+
k, len = 0, length
|
15
|
+
while(len >= 12)
|
16
|
+
a = m(a + bytes[k + 0] + (bytes[k + 1] << 8) + (bytes[k + 2] << 16) + (bytes[k + 3] << 24))
|
17
|
+
b = m(b + bytes[k + 4] + (bytes[k + 5] << 8) + (bytes[k + 6] << 16) + (bytes[k + 7] << 24))
|
18
|
+
c = m(c + bytes[k + 8] + (bytes[k + 9] << 8) + (bytes[k + 10] << 16) + (bytes[k + 11] << 24))
|
19
|
+
|
20
|
+
a, b, c = mix(a, b, c)
|
21
|
+
k += 12
|
22
|
+
len -= 12
|
23
|
+
end
|
24
|
+
|
25
|
+
c = c + length
|
26
|
+
|
27
|
+
c = mix(*toss(a, b, c, bytes, len, k))[2]
|
28
|
+
"6" + c.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def byte_array(string_to_hash)
|
34
|
+
bytes = []
|
35
|
+
string_to_hash.each_byte {|b| bytes << b}
|
36
|
+
bytes
|
37
|
+
end
|
38
|
+
|
39
|
+
# Need to keep numbers in the unsigned int 32 range
|
40
|
+
def m(v)
|
41
|
+
v % 0x100000000
|
42
|
+
end
|
43
|
+
|
44
|
+
def mix(a, b, c)
|
45
|
+
a, b, c = m(a), m(b), m(c)
|
46
|
+
|
47
|
+
a = m(a-b-c) ^ m(c >> 13)
|
48
|
+
b = m(b-c-a) ^ m(a << 8)
|
49
|
+
c = m(c-a-b) ^ m(b >> 13)
|
50
|
+
|
51
|
+
a = m(a-b-c) ^ m(c >> 12)
|
52
|
+
b = m(b-c-a) ^ m(a << 16)
|
53
|
+
c = m(c-a-b) ^ m(b >> 5)
|
54
|
+
|
55
|
+
a = m(a-b-c) ^ m(c >> 3)
|
56
|
+
b = m(b-c-a) ^ m(a << 10)
|
57
|
+
c = m(c-a-b) ^ m(b >> 15)
|
58
|
+
|
59
|
+
[a, b, c]
|
60
|
+
end
|
61
|
+
|
62
|
+
def toss(a, b, c, bytes, len, k)
|
63
|
+
case len
|
64
|
+
when 9..11
|
65
|
+
c = c + (bytes[k+len-1] << ((len % 8) * 8))
|
66
|
+
when 5..8
|
67
|
+
b = b + (bytes[k+len-1] << ((len % 5) * 8))
|
68
|
+
when 1..4
|
69
|
+
a = a + (bytes[k+len-1] << ((len - 1) * 8))
|
70
|
+
else
|
71
|
+
return [a, b, c]
|
72
|
+
end
|
73
|
+
toss(a, b, c, bytes, len-1, k)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: digest_perfect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Starr Horne
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-22 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Among other things, this is the hash used to create requests to google's pagerank tool. Most of the code comes from the excellent PageRankr gem.
|
23
|
+
email:
|
24
|
+
- starr@chromahq.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- digest_perfect.gemspec
|
37
|
+
- lib/digest_perfect.rb
|
38
|
+
- lib/digest_perfect/version.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: https://github.com/starrhorne/digest_perfect
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_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
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: digest_perfect
|
69
|
+
rubygems_version: 1.6.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Implementation of Bob Jenkins' perfect hash algorithm in ruby
|
73
|
+
test_files: []
|
74
|
+
|