digest-sip_hash 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/siphash +18 -7
- data/lib/digest/sip_hash.rb +26 -35
- data/lib/digest/sip_hash/version.rb +3 -1
- data/spec/helper.rb +5 -0
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a7f861280074e2479ea4dd879b0689e7d68c3e1b79cd8b21d2d8a681fe3403e
|
4
|
+
data.tar.gz: 88f8f40d5938dff8c6c7cc07337753e91136b891e04bfeb493b83c52baf8a055
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f6664550a1a456f862e5e4712946dcb199481ea3bdfb639ce6bb3c907dd61a221a7d3aa40fd81363a9d7367a7aa0880d99fa868565813a6b327cb398a1f0431
|
7
|
+
data.tar.gz: 86623e37d7da7e52d2e3d87df72b68700275801ac21d7824b127d33da91d74898f1481f0feb0da264c502cb161bd3b1797fd6093067b12ba96151af2b0e89a9f
|
data/bin/siphash
CHANGED
@@ -6,8 +6,9 @@ require 'digest/sip_hash'
|
|
6
6
|
require 'optionparser'
|
7
7
|
|
8
8
|
hex_key = Digest::SipHash::DEFAULT_KEY.unpack1 'H*'
|
9
|
-
|
10
|
-
|
9
|
+
Config = Struct.new :c, :d, :key, keyword_init: true
|
10
|
+
config = Config.new c: 1, d: 3, key: hex_key
|
11
|
+
|
11
12
|
options = ARGV.options do |x|
|
12
13
|
x.banner = "Usage: #{x.program_name} [OPTIONS] FILE"
|
13
14
|
x.version = Digest::SipHash::VERSION
|
@@ -15,13 +16,23 @@ options = ARGV.options do |x|
|
|
15
16
|
UnsignedInteger = /\A\d+\z/
|
16
17
|
x.accept UnsignedInteger, UnsignedInteger, &:to_i
|
17
18
|
|
18
|
-
|
19
|
-
x.
|
20
|
-
|
19
|
+
HexOctets = /\A\h{32}\z/
|
20
|
+
x.accept HexOctets, HexOctets do |octets|
|
21
|
+
[octets].pack('H*').b
|
22
|
+
end
|
23
|
+
|
24
|
+
x.on '-c=ROUNDS', UnsignedInteger, "Number of c rounds. Default: #{config.c}"
|
25
|
+
x.on '-d=ROUNDS', UnsignedInteger, "Number of d rounds. Default: #{config.d}"
|
26
|
+
x.on '-k', '--key=KEY', HexOctets, "Sixteen-byte hex key. Default: \"#{hex_key}\""
|
21
27
|
end.freeze
|
22
|
-
|
28
|
+
|
29
|
+
begin
|
30
|
+
options.permute! into: config
|
31
|
+
rescue OptionParser::ParseError => e
|
32
|
+
abort e.message
|
33
|
+
end
|
23
34
|
|
24
35
|
path = ARGV.fetch 0, '-'
|
25
|
-
hash = Digest::SipHash.new(
|
36
|
+
hash = Digest::SipHash.new(config.c, config.d, key: config.key).hexdigest ARGF.read
|
26
37
|
|
27
38
|
puts "#{hash} #{path}"
|
data/lib/digest/sip_hash.rb
CHANGED
@@ -56,9 +56,9 @@ module Digest
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def transform
|
59
|
-
return
|
60
|
-
(@size / 8).times { |n|
|
61
|
-
|
59
|
+
return compress_block 0 if @size.zero?
|
60
|
+
(@size / 8).times { |n| compress_block block n }
|
61
|
+
compress_block last_block
|
62
62
|
end
|
63
63
|
|
64
64
|
def finalize
|
@@ -69,53 +69,44 @@ module Digest
|
|
69
69
|
|
70
70
|
private
|
71
71
|
|
72
|
-
def
|
73
|
-
@v3 ^=
|
72
|
+
def compress_block n
|
73
|
+
@v3 ^= n
|
74
74
|
@c_rounds.times { compress }
|
75
|
-
@v0 ^=
|
75
|
+
@v0 ^= n
|
76
76
|
end
|
77
77
|
|
78
|
-
def
|
78
|
+
def block n
|
79
79
|
@buffer.slice(n * 8, 8).unpack1 'Q<'
|
80
80
|
end
|
81
81
|
|
82
|
-
def
|
82
|
+
def last_block
|
83
83
|
remainder = @size % 8
|
84
84
|
offset = @size - remainder
|
85
85
|
|
86
|
-
|
87
|
-
|
88
|
-
7.downto 0 do |n|
|
89
|
-
next if n >= remainder
|
90
|
-
last |= @buffer[n + offset].ord << 8 * n
|
86
|
+
remainder.times.reverse_each.reduce @size << 56 & MASK do |last, n|
|
87
|
+
last | @buffer[n + offset].ord << 8 * n
|
91
88
|
end
|
92
|
-
|
93
|
-
last
|
94
89
|
end
|
95
90
|
|
96
91
|
def compress
|
97
|
-
@v0
|
98
|
-
@
|
99
|
-
@
|
100
|
-
@
|
101
|
-
@
|
102
|
-
@
|
103
|
-
@
|
104
|
-
@
|
105
|
-
@
|
106
|
-
@
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
@v2 &= MASK
|
112
|
-
@v1 = rotate @v1, by: 17
|
113
|
-
@v1 ^= @v2
|
114
|
-
@v2 = rotate @v2, by: 32
|
92
|
+
@v0 = add @v0, @v1
|
93
|
+
@v1 = rotate @v1, 13, @v0
|
94
|
+
@v0 = rotate @v0, 32
|
95
|
+
@v2 = add @v2, @v3
|
96
|
+
@v3 = rotate @v3, 16, @v2
|
97
|
+
@v0 = add @v0, @v3
|
98
|
+
@v3 = rotate @v3, 21, @v0
|
99
|
+
@v2 = add @v2, @v1
|
100
|
+
@v1 = rotate @v1, 17, @v2
|
101
|
+
@v2 = rotate @v2, 32
|
102
|
+
end
|
103
|
+
|
104
|
+
def add a, b
|
105
|
+
a + b & MASK
|
115
106
|
end
|
116
107
|
|
117
|
-
def rotate n, by
|
118
|
-
n << by & MASK | n >> 64 - by
|
108
|
+
def rotate n, by, xor = 0
|
109
|
+
n << by & MASK | n >> 64 - by ^ xor
|
119
110
|
end
|
120
111
|
end
|
121
112
|
end
|
data/spec/helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: digest-sip_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shannon Skipper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -38,7 +38,21 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '5'
|
41
|
-
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-proveit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1'
|
55
|
+
description: An implementation of SipHash 1-3, 2-4, etc. in pure Ruby.
|
42
56
|
email:
|
43
57
|
- shannonskipper@gmail.com
|
44
58
|
executables:
|