siphash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +4 -0
  4. data/lib/siphash.rb +94 -0
  5. data/spec/siphash_spec.rb +37 -0
  6. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 71976d27ed6eb1972f36bbe19b954fbe97362419
4
+ data.tar.gz: 3758ec6d376c4061d68acfbc811f7abc591b5a60
5
+ SHA512:
6
+ metadata.gz: d5594c152cc8013e0bc4d89decd05ceb1162de8fb80d785936b573eb677cd37d94d089798787ed03834dc9002f8cb3adc5e0279f1dc9f261f2d337585d6413b1
7
+ data.tar.gz: 80ba4c8b69d0c113b02c5a5111768c77a7a74a1a4ab666bf829a7c485cb96e546256d6240a9f849c9763d44cb2525539b9976887945b26dc696b27f94625a351
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Martin Boßlet
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ ## siphash-ruby
2
+
3
+ A Ruby implementation of SipHash[http://131002.net/siphash/siphash.pdf].
4
+ Contains an implementation of SipHash-2-4.
data/lib/siphash.rb ADDED
@@ -0,0 +1,94 @@
1
+ module SipHash
2
+
3
+ def self.digest(key, msg)
4
+ s = State.new(key)
5
+ len = msg.size
6
+ iter = len / 8
7
+
8
+ iter.times do |i|
9
+ m = msg.slice(i * 8, 8).unpack("Q<")[0]
10
+ s.apply_block(m)
11
+ end
12
+
13
+ m = last_block(msg, len, iter)
14
+
15
+ s.apply_block(m)
16
+ s.finalize
17
+ s.digest
18
+ end
19
+
20
+ private
21
+
22
+ def self.last_block(msg, len, iter)
23
+ last = (len << 56) & State::MASK_64;
24
+
25
+ r = len % 8
26
+ off = iter * 8
27
+
28
+ last |= msg[off + 6].ord << 48 if r >= 7
29
+ last |= msg[off + 5].ord << 40 if r >= 6
30
+ last |= msg[off + 4].ord << 32 if r >= 5
31
+ last |= msg[off + 3].ord << 24 if r >= 4
32
+ last |= msg[off + 2].ord << 16 if r >= 3
33
+ last |= msg[off + 1].ord << 8 if r >= 2
34
+ last |= msg[off].ord if r >= 1
35
+ last
36
+ end
37
+
38
+ class State
39
+
40
+ MASK_64 = 0xffffffffffffffff
41
+
42
+ def initialize(key)
43
+ @v0 = 0x736f6d6570736575
44
+ @v1 = 0x646f72616e646f6d
45
+ @v2 = 0x6c7967656e657261
46
+ @v3 = 0x7465646279746573
47
+
48
+ k0 = key.slice(0, 8).unpack("Q<")[0]
49
+ k1 = key.slice(8, 8).unpack("Q<")[0]
50
+
51
+ @v0 ^= k0
52
+ @v1 ^= k1
53
+ @v2 ^= k0
54
+ @v3 ^= k1
55
+ end
56
+
57
+ def apply_block(m)
58
+ @v3 ^= m
59
+ 2.times { compress }
60
+ @v0 ^= m
61
+ end
62
+
63
+ def rotl64(num, shift)
64
+ ((num << shift) & MASK_64) | (num >> (64 - shift))
65
+ end
66
+
67
+ def compress
68
+ @v0 = (@v0 + @v1) & MASK_64
69
+ @v2 = (@v2 + @v3) & MASK_64
70
+ @v1 = rotl64(@v1, 13)
71
+ @v3 = rotl64(@v3, 16)
72
+ @v1 ^= @v0
73
+ @v3 ^= @v2
74
+ @v0 = rotl64(@v0, 32)
75
+ @v2 = (@v2 + @v1) & MASK_64
76
+ @v0 = (@v0 + @v3) & MASK_64
77
+ @v1 = rotl64(@v1, 17)
78
+ @v3 = rotl64(@v3, 21)
79
+ @v1 ^= @v2
80
+ @v3 ^= @v0
81
+ @v2 = rotl64(@v2, 32)
82
+ end
83
+
84
+ def finalize
85
+ @v2 ^= 0xff
86
+ 4.times { compress }
87
+ end
88
+
89
+ def digest
90
+ @v0 ^ @v1 ^ @v2 ^ @v3
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,37 @@
1
+ require 'rspec'
2
+ require 'siphash'
3
+
4
+ describe SipHash do
5
+
6
+ let(:spec_key) { "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" }
7
+ let(:spec_msg) { "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" }
8
+
9
+ specify "Test vector from spec" do
10
+ SipHash.digest(spec_key, spec_msg).should == 0xa129ca6149be45e5
11
+ end
12
+
13
+ specify "empty string" do
14
+ SipHash.digest(spec_key, "").should == 0x726fdb47dd0e0e31
15
+ end
16
+
17
+ specify "a" do
18
+ SipHash.digest(spec_key, "a").should == 0x2ba3e8e9a71148ca
19
+ end
20
+
21
+ specify "abcdef" do
22
+ SipHash.digest(spec_key, "abcdef").should == 0x2a6e77e733c7c05d
23
+ end
24
+
25
+ specify "SipHash" do
26
+ SipHash.digest(spec_key, "SipHash").should == 0x8325093242a96f60
27
+ end
28
+
29
+ specify "12345678" do
30
+ SipHash.digest(spec_key, "12345678").should == 0x2130609caea37eb
31
+ end
32
+
33
+ specify "one million zero bytes" do
34
+ SipHash.digest(spec_key, "\0" * 1000000).should == 0x28205108397aa742
35
+ end
36
+
37
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siphash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Martin Bosslet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby implementation of SipHash.
14
+ email: Martin.Bosslet@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/siphash.rb
23
+ - spec/siphash_spec.rb
24
+ homepage: https://github.com/emboss/siphash-ruby
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.9.3
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.2.0.preview.1
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: A Ruby implementation of SipHash.
48
+ test_files: []
49
+ has_rdoc: