gibberish 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gibberish (0.0.2)
4
+ gibberish (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -7,43 +7,82 @@ module Gibberish
7
7
  # Gibberish::SHA1("data") #=> a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd
8
8
  # Gibberish::SHA256("data") #=> 3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7
9
9
  #
10
+ # ## OpenSSL CLI Interop
11
+ #
12
+ # echo -n 'data' | openssl dgst -sha1
13
+ # echo -n 'data' | openssl dgst -sha256
14
+ # echo -n 'data' | openssl dgst -md5
15
+ #
16
+ # is the same as
17
+ #
18
+ # Gibberish::SHA1("data")
19
+ # Gibberish::SHA256("data")
20
+ # Gibberish::MD5("data")
21
+ #
10
22
  class Digest
11
23
 
12
- def self.sha1(val, opts={})
24
+ # Returns the SHA1 digest for the data
25
+ #
26
+ # Shorcut alias: Gibberish::SHA1(data)
27
+ #
28
+ # @param [String] key
29
+ # @param [#to_s] data
30
+ # @param [Hash] options
31
+ # @option opts [Boolean] :binary (false) encode the data in binary, not Base64
32
+ def self.sha1(data, opts={})
33
+ data = data.to_s
13
34
  if opts[:binary]
14
- OpenSSL::Digest::SHA1.digest(val)
35
+ OpenSSL::Digest::SHA1.digest(data)
15
36
  else
16
- OpenSSL::Digest::SHA1.hexdigest(val)
37
+ OpenSSL::Digest::SHA1.hexdigest(data)
17
38
  end
18
39
  end
19
40
 
20
- def self.sha256(val, opts={})
41
+ # Returns the SHA256 digest for the data
42
+ #
43
+ # Shorcut alias: Gibberish::SHA256(data)
44
+ #
45
+ # @param [String] key
46
+ # @param [#to_s] data
47
+ # @param [Hash] options
48
+ # @option opts [Boolean] :binary (false) encode the data in binary, not Base64
49
+ def self.sha256(data, opts={})
50
+ data = data.to_s
21
51
  if opts[:binary]
22
- OpenSSL::Digest::SHA256.digest(val)
52
+ OpenSSL::Digest::SHA256.digest(data)
23
53
  else
24
- OpenSSL::Digest::SHA256.hexdigest(val)
54
+ OpenSSL::Digest::SHA256.hexdigest(data)
25
55
  end
26
56
  end
27
57
 
28
- def self.md5(val, opts={})
58
+ # Returns the MD5 digest for the data
59
+ #
60
+ # Shorcut alias: Gibberish::MD5(data)
61
+ #
62
+ # @param [String] key
63
+ # @param [#to_s] data
64
+ # @param [Hash] options
65
+ # @option opts [Boolean] :binary (false) encode the data in binary, not Base64
66
+ def self.md5(data, opts={})
67
+ data = data.to_s
29
68
  if opts[:binary]
30
- OpenSSL::Digest::MD5.digest(val)
69
+ OpenSSL::Digest::MD5.digest(data)
31
70
  else
32
- OpenSSL::Digest::MD5.hexdigest(val)
71
+ OpenSSL::Digest::MD5.hexdigest(data)
33
72
  end
34
73
  end
35
74
  end
36
75
 
37
- def self.SHA1(val, opts={})
38
- Digest.sha1(val,opts)
76
+ def self.SHA1(data, opts={})
77
+ Digest.sha1(data,opts)
39
78
  end
40
79
 
41
- def self.SHA256(val, opts={})
42
- Digest.sha256(val,opts)
80
+ def self.SHA256(data, opts={})
81
+ Digest.sha256(data,opts)
43
82
  end
44
83
 
45
- def self.MD5(val, opts={})
46
- Digest.md5(val,opts)
84
+ def self.MD5(data, opts={})
85
+ Digest.md5(data,opts)
47
86
  end
48
87
 
49
88
  end
@@ -7,13 +7,31 @@ module Gibberish
7
7
  # Gibberish::HMAC('key', 'data', :digest => :sha256)
8
8
  # #=> 5031fe3d989c6d1537a013fa6e739da23463fdaec3b70137d828e36ace221bd0
9
9
  #
10
+ # ## OpenSSL CLI Interop
11
+ #
12
+ # echo -n "stuff" | openssl dgst -sha1 -hmac 'password'
13
+ #
14
+ # is the same as
15
+ #
16
+ # Gibberish::HMAC('password', 'stuff')
17
+ #
10
18
  class HMAC
11
19
  DIGEST = {
12
20
  :sha1 => OpenSSL::Digest::Digest.new('sha1'),
13
21
  :sha256 => OpenSSL::Digest::Digest.new('sha256')
14
22
  }
15
23
 
24
+ # Returns the HMAC for the key and data
25
+ #
26
+ # Shorcut alias: Gibberish::HMAC(key, data)
27
+ #
28
+ # @param [String] key
29
+ # @param [#to_s] data
30
+ # @param [Hash] options
31
+ # @option opts [Symbol] :digest (:sha1) the digest to encode with
32
+ # @option opts [Boolean] :binary (false) encode the data in binary, not Base64
16
33
  def self.digest(key, data, opts={})
34
+ data = data.to_s
17
35
  digest_type = opts[:digest] || :sha1
18
36
  if opts[:binary]
19
37
  OpenSSL::HMAC.digest(DIGEST[digest_type], key, data)
@@ -1,3 +1,3 @@
1
1
  module Gibberish
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/spec/hmac_spec.rb CHANGED
@@ -6,4 +6,10 @@ describe "HMAC" do
6
6
  Gibberish::HMAC("password", "data").must_equal("08d13c72bed7ace5efadc09df109a78a5d713097")
7
7
  end
8
8
 
9
+ it "should work with OpenSSL HMAC" do
10
+ hmac = Gibberish::HMAC("password", "data\n")
11
+ o_hmac = `echo "data" | openssl dgst -sha1 -hmac 'password'`
12
+ hmac.must_equal(o_hmac.chomp)
13
+ end
14
+
9
15
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gibberish
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mark Percival