p_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.md +7 -0
  4. data/README.md +61 -0
  5. data/lib/p_hash.rb +44 -0
  6. data/p_hash.gemspec +15 -0
  7. metadata +63 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f505aba16a78963631113602c33379717a6d0d56a28bbb1cd7c5ebaa4baba2ee
4
+ data.tar.gz: 86d726e734513248ffa7f770e7490c35229979f4e44952e517065a1cc85a8491
5
+ SHA512:
6
+ metadata.gz: 8edc2fbf65f3181bd0804e0eea9a5f77e23481694761e4980205c8382e2b6709307795949c89a530d4520d8974876603c240f2f1f27ca984916513056e51e52f
7
+ data.tar.gz: 8c2f35f5f55fdbe0b358481ad1838a866902b172bc88f862788e79eb09a6eef57e2a040d1d2ec47a31b861c0aa3deebc821daf053b2f74a0717ef3113e34b20e
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,7 @@
1
+ Copyright 2017 Jonathan Hooper
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ This gem provides a P_hash data expansion function as described by [Section 5 of RFC 5246](https://tools.ietf.org/html/rfc5246#section-5).
2
+
3
+ # Usage
4
+
5
+ To calculate `P_SHA1`:
6
+
7
+ ```ruby
8
+ hash_function = OpenSSL::Digest::SHA1.new
9
+ secret = 'asdf1234' # Also referred to as the "client secret" for WS-Trust
10
+ seed = 'qwer5678' # Also referred to as the "server secret" for WS-Trust
11
+
12
+ digest = PHash.new(hash_function, secret, seed)
13
+ ```
14
+
15
+ ### Specifying a hash function
16
+
17
+ For `P_SHA256`, `P_MD5`, or similar, simply initialize `PHash` with the appropriate hashing function:
18
+
19
+ ```ruby
20
+ hash_function = OpenSSL::Digest::SHA256.new
21
+ PHash.new(hash_function, 'asdf1234', 'qwer5678').digest
22
+ ```
23
+
24
+ ### Key sizes
25
+
26
+ The default key size is 32 bytes. To use a different key size, specify that as the fourth argument:
27
+
28
+ ```ruby
29
+ PHash.new(OpenSSL::Digest::SHA256.new, 'asdf1234', 'qwer5678', 64).digest # Results in a 64 byte key
30
+ ```
31
+
32
+ ### Base64 and hex digests
33
+
34
+ As a convenience, the `PHash` class also offers a `base64digest` and `hexdigest` method:
35
+
36
+ ```ruby
37
+ hash_function = OpenSSL::Digest::SHA256.new
38
+ p_hash = PHash.new(hash_function, 'asdf1234', 'qwer5678')
39
+
40
+ p_hash.base64digest # Base64 encoded digest
41
+ p_hash.hexdigest # Hex encoded digest
42
+ ```
43
+
44
+ # The P_hash function
45
+
46
+ Per RFC 5246, `P_hash()` is described as follows:
47
+
48
+ ```
49
+ p_hash(client_secret, server_secret) = HMAC_hash(secret, A(1) + seed) +
50
+ HMAC_hash(secret, A(2) + seed) +
51
+ HMAC_hash(secret, A(3) + seed) + ...
52
+ ```
53
+
54
+ Here '+' indicates concatenation.
55
+
56
+ `A()` is defined by:
57
+
58
+ ```
59
+ A(0) = seed
60
+ A(i) = HMAC_hash(secret, A(i-1))
61
+ ```
@@ -0,0 +1,44 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+
4
+ class PHash
5
+ attr_reader :hash_function, :secret, :seed, :key_length, :digest
6
+
7
+ def initialize(hash_function, secret, seed, key_length = 32)
8
+ @hash_function = hash_function
9
+ @secret = secret
10
+ @seed = seed
11
+ @key_length = key_length
12
+ calculate_digest
13
+ end
14
+
15
+ def hexdigest
16
+ @hexdigest ||= digest.unpack('H*').first
17
+ end
18
+
19
+ def base64digest
20
+ @base64digest ||= Base64.strict_encode64(digest)
21
+ end
22
+
23
+ private
24
+
25
+ def calculate_digest
26
+ digest = ''
27
+ while digest.length < key_length
28
+ digest = digest + OpenSSL::HMAC.digest(
29
+ hash_function,
30
+ secret,
31
+ next_a_value + seed
32
+ )
33
+ end
34
+ @digest = digest[0...key_length]
35
+ end
36
+
37
+ def next_a_value
38
+ @a_value = OpenSSL::HMAC.digest(
39
+ hash_function,
40
+ secret,
41
+ @a_value || seed
42
+ )
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'p_hash'
5
+ s.version = '0.0.1'
6
+ s.licenses = ['MIT']
7
+ s.summary = "A gem that provides an implementation of p_hash"
8
+ s.description = "This is a gem that provides an implementation of the p_hash function as described in the TLS specification"
9
+ s.authors = ["Jonathan Hooper"]
10
+ s.email = 'jon@jonathanhooper.net'
11
+ s.files = Dir.glob('lib/**/*') + ['LICENSE.md', 'README.md', 'Gemfile', 'p_hash.gemspec']
12
+ s.homepage = 'https://github.com/jmhooper/p_hash'
13
+
14
+ s.add_development_dependency 'rspec'
15
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: p_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Hooper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This is a gem that provides an implementation of the p_hash function
28
+ as described in the TLS specification
29
+ email: jon@jonathanhooper.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - LICENSE.md
36
+ - README.md
37
+ - lib/p_hash.rb
38
+ - p_hash.gemspec
39
+ homepage: https://github.com/jmhooper/p_hash
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.7.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: A gem that provides an implementation of p_hash
63
+ test_files: []