mac 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bdaba057d3367392da97d8afd0cd5f1b5973686b97c3028757eb50f11d8f8c7
4
- data.tar.gz: 78fcc37df2f60f67c8eeeddf28619931b0f55f0f61b9a72bc8dcc06234962d04
3
+ metadata.gz: 4877cf61c1e7df75ab432af4cd0e78278390f0ac8a61d2a59d8bd51208f75207
4
+ data.tar.gz: 8f622f976919faaabfbccd5bcc0dade0c3166eaf6750fb7bc02cfc02f0742d6f
5
5
  SHA512:
6
- metadata.gz: 64fd0408a3848dcb93734bd80b230e6a2ff25f574bcb4e8289144332eb5cde51f9077da7eea7a96c89060c039a53b7ff9d5a4583fd9074820b18e83481fd961b
7
- data.tar.gz: 0eba94242ffe369676dea2c52a2924410a5ef0d832990763bde0387de299603d539688e60551be6521d2e3290d8f56701b38a1130e62c911cb395b3474598616
6
+ metadata.gz: c881e11c07e4d9cd8045e5df534372998ef617ac8960f9b5340995ddd068370c0334ec290af510cf70653ec0b665ea5b686bb969b29854de4b7f13647828659c
7
+ data.tar.gz: 4575cac089235b92daa5ac448752e30da47e0525fd110b8eb00f91da428534233502886159bdf6683532df3c964975ede91d11534cc4496b1d77400d93a8f1f8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,7 @@
1
- ## [Unreleased]
1
+ ## [1.0.1] - 2026-06-17
2
2
 
3
- ## [0.1.0] - 2026-05-24
3
+ - Add tests with 100% code coverage
4
4
 
5
- - Initial release
5
+ ## [1.0.0] - 2026-05-24
6
+
7
+ - Initial release: Mac class with `sign` and `verify` methods.
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2026 claudiob
3
+ Copyright (c) 2026 HouseAccount
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,39 +1,22 @@
1
- # Mac
1
+ # Message Authentication Code (MAC) with SHA256 and signature
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ ## Available methods
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mac`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ Sign a message:
6
6
 
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
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
- If bundler is not being used to manage dependencies, install the gem by executing:
13
+ Verify a signed message:
18
14
 
19
- ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```ruby
16
+ mac = Mac.new message:, secret:
17
+ mac.signed? signature:, timestamp: # true
21
18
  ```
22
19
 
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
20
+ ## Tests
38
21
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
22
+ Run `bundle exec ruby -Itest test/mac_test.rb`
data/lib/mac.rb CHANGED
@@ -1,8 +1,39 @@
1
- # frozen_string_literal: true
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'rack'
2
4
 
3
- require_relative "mac/version"
5
+ # Provides methods to sign and verify timestamped messages with HMAC SHA256.
6
+ class Mac
7
+ # Sets up a message to be signed/verified with a secret.
8
+ def initialize(message:, secret:, timed: true)
9
+ @message = message
10
+ @secret = secret
11
+ @timed = timed
12
+ end
4
13
 
5
- module Mac
6
- class Error < StandardError; end
7
- # Your code goes here...
14
+ # Sets up a message and calculates its current signature.
15
+ def self.sign(message:, secret:, timed: true, hexdigest: true)
16
+ new(message: message, secret: secret).tap do |mac|
17
+ mac.sign timestamp: (Time.now if timed), hexdigest: hexdigest
18
+ end
19
+ end
20
+
21
+ attr_reader :signature, :timestamp
22
+
23
+ # Returns whether the provided signature and timestamp match the signature of the message.
24
+ def signed?(signature:, timestamp: nil, hexdigest: true)
25
+ sign hexdigest: hexdigest, timestamp: timestamp
26
+ Rack::Utils.secure_compare @signature, signature
27
+ end
28
+
29
+ # Calculates the signature of the message.
30
+ def sign(timestamp: nil, hexdigest: true)
31
+ @timestamp = timestamp.to_i.to_s if timestamp
32
+ payload = [@timestamp, @message].compact.join '.'
33
+ @signature = if hexdigest
34
+ OpenSSL::HMAC.hexdigest 'SHA256', @secret, payload
35
+ else
36
+ Base64.strict_encode64(OpenSSL::HMAC.digest 'SHA256', @secret, payload)
37
+ end
38
+ end
8
39
  end
data/test/mac_test.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'simplecov'
2
+ SimpleCov.start { minimum_coverage 100 }
3
+ require 'minitest/autorun'
4
+
5
+ require_relative '../lib/mac'
6
+
7
+ class MacTest < Minitest::Test
8
+ def test_with_hexdigest
9
+ mac = Mac.sign message: { foo: :bar }.to_json, secret: 'S3kr3t'
10
+ assert mac.signed? signature: mac.signature, timestamp: mac.timestamp
11
+ end
12
+
13
+ def test_without_hexdigest
14
+ mac = Mac.sign message: { foo: :bar }.to_json, secret: 'S3kr3t', hexdigest: false
15
+ assert mac.signed? signature: mac.signature, timestamp: mac.timestamp, hexdigest: false
16
+ end
17
+ end
metadata CHANGED
@@ -1,14 +1,84 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.1
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: base64
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'
26
+ - !ruby/object:Gem::Dependency
27
+ name: openssl
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rack
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: minitest
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: simplecov
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
12
82
  description: Enhances OpenSSL::HMAC with timestamp.
13
83
  email:
14
84
  - claudiob@users.noreply.github.com
@@ -19,10 +89,8 @@ files:
19
89
  - CHANGELOG.md
20
90
  - LICENSE.txt
21
91
  - README.md
22
- - Rakefile
23
92
  - lib/mac.rb
24
- - lib/mac/version.rb
25
- - sig/mac.rbs
93
+ - test/mac_test.rb
26
94
  homepage: https://github.com/HouseAccountEng/mac
27
95
  licenses:
28
96
  - MIT
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- task default: :spec
data/lib/mac/version.rb DELETED
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mac
4
- VERSION = "0.1.0"
5
- end
data/sig/mac.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Mac
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end