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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97550c1117af3f364f69b7cf6fe0811b98abe710c1de440d75d43e2d8da30fc9
4
- data.tar.gz: 50f608c0b241a416890727a44be29dda80ba78e213c7b74cee9e114dfed62822
3
+ metadata.gz: 5a7f861280074e2479ea4dd879b0689e7d68c3e1b79cd8b21d2d8a681fe3403e
4
+ data.tar.gz: 88f8f40d5938dff8c6c7cc07337753e91136b891e04bfeb493b83c52baf8a055
5
5
  SHA512:
6
- metadata.gz: 3e1ce5568375369e28fe69708ae0780d29e74c7859d3df3e8ae248e484a8f6c13d2d851c932a3084831deaf3f8ca7b05f75184bd616a2716151cd08ea9fbd30e
7
- data.tar.gz: 47537c8a82aae0818eb6eeea9e879bc6d33e6f340e70efa11d2cb14f6a267bbe1a54bb7b8b651afacd23be918ec6d97133e422706c82dfbd9c0b519bec7e1694
6
+ metadata.gz: 5f6664550a1a456f862e5e4712946dcb199481ea3bdfb639ce6bb3c907dd61a221a7d3aa40fd81363a9d7367a7aa0880d99fa868565813a6b327cb398a1f0431
7
+ data.tar.gz: 86623e37d7da7e52d2e3d87df72b68700275801ac21d7824b127d33da91d74898f1481f0feb0da264c502cb161bd3b1797fd6093067b12ba96151af2b0e89a9f
@@ -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
- Settings = Struct.new :c, :d, :key, keyword_init: true
10
- settings = Settings.new c: 1, d: 3, key: hex_key
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
- x.on '-c=ROUNDS', UnsignedInteger, "Number of c rounds. Default: #{settings.c}"
19
- x.on '-d=ROUNDS', UnsignedInteger, "Number of d rounds. Default: #{settings.d}"
20
- x.on '-k', '--key=KEY', String, "Sixteen-byte hex key. Default: \"#{hex_key}\""
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
- options.permute! into: settings
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(settings.c, settings.d, key: [settings.key].pack('H*')).hexdigest ARGF.read
36
+ hash = Digest::SipHash.new(config.c, config.d, key: config.key).hexdigest ARGF.read
26
37
 
27
38
  puts "#{hash} #{path}"
@@ -56,9 +56,9 @@ module Digest
56
56
  end
57
57
 
58
58
  def transform
59
- return compress_word 0 if @size.zero?
60
- (@size / 8).times { |n| compress_word word n }
61
- compress_word last_word
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 compress_word m
73
- @v3 ^= m
72
+ def compress_block n
73
+ @v3 ^= n
74
74
  @c_rounds.times { compress }
75
- @v0 ^= m
75
+ @v0 ^= n
76
76
  end
77
77
 
78
- def word n
78
+ def block n
79
79
  @buffer.slice(n * 8, 8).unpack1 'Q<'
80
80
  end
81
81
 
82
- def last_word
82
+ def last_block
83
83
  remainder = @size % 8
84
84
  offset = @size - remainder
85
85
 
86
- last = @size << 56 & MASK
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 += @v1
98
- @v0 &= MASK
99
- @v1 = rotate @v1, by: 13
100
- @v1 ^= @v0
101
- @v0 = rotate @v0, by: 32
102
- @v2 += @v3
103
- @v2 &= MASK
104
- @v3 = rotate @v3, by: 16
105
- @v3 ^= @v2
106
- @v0 += @v3
107
- @v0 &= MASK
108
- @v3 = rotate @v3, by: 21
109
- @v3 ^= @v0
110
- @v2 += @v1
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
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'digest'
4
+
3
5
  module Digest
4
6
  class SipHash < Digest::Class
5
- VERSION = '0.0.2'
7
+ VERSION = '0.0.3'
6
8
  end
7
9
  end
@@ -5,4 +5,9 @@ $LOAD_PATH.prepend lib unless $LOAD_PATH.include? lib
5
5
 
6
6
  require 'digest/sip_hash'
7
7
  require 'minitest/autorun'
8
+ require 'minitest/hell'
8
9
  require 'minitest/pride'
10
+
11
+ class Minitest::Test
12
+ prove_it!
13
+ end
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.2
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-21 00:00:00.000000000 Z
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
- description: An implementation of SipHash 1-3 and 2-4 in pure Ruby.
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: