mac 0.1.0 → 1.0.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -4
- data/LICENSE.txt +1 -1
- data/README.md +11 -32
- data/lib/mac.rb +32 -5
- metadata +16 -5
- data/Rakefile +0 -8
- data/lib/mac/version.rb +0 -5
- data/sig/mac.rbs +0 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e8c78e4cd9f0ee48b31be15a0c68692735c6c6ef4bab6b68787c0b65052e2d10
|
|
4
|
+
data.tar.gz: 2d7189ac699222bc97b1a0c80cafa183e174b37fec38b1d834d5c3be3e3da445
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d221e300acfc8578f3dd2d148304a24444c8ccac090e2c605c02db46c475d35f0b94f88be56f2b67bbbd292893608d18cf322dbadb86fb2cc3d0c9bda3714329
|
|
7
|
+
data.tar.gz: '084d2e9304f1a405afe9cfecb2ead7bc47c8eeaf09947577c3d9d7b2775bba148a5c31c8b05b099c273f9958eb093dac8288acaea720b824b533d4383c4bb16d'
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -1,39 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Message Authentication Code (MAC) with SHA256 and signature
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Available methods
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Sign a message:
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
7
|
+
```ruby
|
|
8
|
+
mac = Mac.sign message:, secret:
|
|
9
|
+
mac.timestamp # => 1779489515999
|
|
10
|
+
mac.signature # => 46f5297a94d0050ba6039bfcb12d6e4c1f955e39b34f98cf2bd5f9720b34ac49
|
|
15
11
|
```
|
|
16
12
|
|
|
17
|
-
|
|
13
|
+
Verify a signed message:
|
|
18
14
|
|
|
19
|
-
```
|
|
20
|
-
|
|
15
|
+
```ruby
|
|
16
|
+
mac = Mac.new message:, secret:
|
|
17
|
+
mac.signed? signature:, timestamp: # true
|
|
21
18
|
```
|
|
22
|
-
|
|
23
|
-
## Usage
|
|
24
|
-
|
|
25
|
-
TODO: Write usage instructions here
|
|
26
|
-
|
|
27
|
-
## Development
|
|
28
|
-
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
-
|
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
-
|
|
33
|
-
## Contributing
|
|
34
|
-
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mac.
|
|
36
|
-
|
|
37
|
-
## License
|
|
38
|
-
|
|
39
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/mac.rb
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Provides methods to sign and verify timestamped messages with HMAC SHA256.
|
|
2
|
+
class Mac
|
|
3
|
+
# Sets up a message to be signed/verified with a secret.
|
|
4
|
+
def initialize(message:, secret:, timed: true)
|
|
5
|
+
@message = message
|
|
6
|
+
@secret = secret
|
|
7
|
+
@timed = timed
|
|
8
|
+
end
|
|
2
9
|
|
|
3
|
-
|
|
10
|
+
# Sets up a message and calculates its current signature.
|
|
11
|
+
def self.sign(message:, secret:, timed: true, hexdigest: true)
|
|
12
|
+
new(message: message, secret: secret).tap do |mac|
|
|
13
|
+
mac.sign timestamp: (Time.now if timed), hexdigest: hexdigest
|
|
14
|
+
end
|
|
15
|
+
end
|
|
4
16
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
#
|
|
17
|
+
attr_reader :signature, :timestamp
|
|
18
|
+
|
|
19
|
+
# Returns whether the provided signature and timestamp match the signature of the message.
|
|
20
|
+
def signed?(signature:, timestamp: nil, hexdigest: true)
|
|
21
|
+
sign hexdigest: hexdigest, timestamp: timestamp
|
|
22
|
+
Rack::Utils.secure_compare @signature, signature
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Calculates the signature of the message.
|
|
26
|
+
def sign(timestamp: nil, hexdigest: true)
|
|
27
|
+
@timestamp = timestamp.to_i.to_s if timestamp
|
|
28
|
+
payload = [@timestamp, @message].compact.join '.'
|
|
29
|
+
@signature = if hexdigest
|
|
30
|
+
OpenSSL::HMAC.hexdigest 'SHA256', @secret, payload
|
|
31
|
+
else
|
|
32
|
+
Base64.strict_encode64(OpenSSL::HMAC.digest 'SHA256', @secret, payload)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
8
35
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mac
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- claudiob
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
-
dependencies:
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rack
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
12
26
|
description: Enhances OpenSSL::HMAC with timestamp.
|
|
13
27
|
email:
|
|
14
28
|
- claudiob@users.noreply.github.com
|
|
@@ -19,10 +33,7 @@ files:
|
|
|
19
33
|
- CHANGELOG.md
|
|
20
34
|
- LICENSE.txt
|
|
21
35
|
- README.md
|
|
22
|
-
- Rakefile
|
|
23
36
|
- lib/mac.rb
|
|
24
|
-
- lib/mac/version.rb
|
|
25
|
-
- sig/mac.rbs
|
|
26
37
|
homepage: https://github.com/HouseAccountEng/mac
|
|
27
38
|
licenses:
|
|
28
39
|
- MIT
|
data/Rakefile
DELETED
data/lib/mac/version.rb
DELETED
data/sig/mac.rbs
DELETED