-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HMAC using RbNaCL separated into own implementations.
- HMAC using OpenSSL (default) - HMAC with RbNaCl for keys under 32 chars (rbnacl < 6.0) - HMAC with RbNaCl (rbnacl >= 6.0)
- Loading branch information
Showing
18 changed files
with
301 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "rubocop", "< 1.32" | ||
gem "rbnacl", "< 6" | ||
|
||
gemspec path: "../" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,6 @@ | |
source "https://rubygems.org" | ||
|
||
gem "rubocop", "< 1.32" | ||
gem "rbnacl" | ||
gem "rbnacl", ">= 6" | ||
|
||
gemspec path: "../" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "rubocop", "< 1.32" | ||
gem "rbnacl", "< 6" | ||
|
||
gemspec path: "../" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module Algos | ||
module HmacRbNaCl | ||
module_function | ||
|
||
MAPPING = { | ||
'HS256' => ::RbNaCl::HMAC::SHA256, | ||
'HS512256' => ::RbNaCl::HMAC::SHA512256, | ||
'HS384' => nil, | ||
'HS512' => ::RbNaCl::HMAC::SHA512 | ||
}.freeze | ||
|
||
SUPPORTED = MAPPING.keys | ||
|
||
def sign(algorithm, msg, key) | ||
if (hmac = resolve_algorithm(algorithm)) | ||
hmac.auth(key_for_rbnacl(hmac, key).encode('binary'), msg.encode('binary')) | ||
else | ||
Hmac.sign(algorithm, msg, key) | ||
end | ||
end | ||
|
||
def verify(algorithm, key, signing_input, signature) | ||
if (hmac = resolve_algorithm(algorithm)) | ||
hmac.verify(key_for_rbnacl(hmac, key).encode('binary'), signature.encode('binary'), signing_input.encode('binary')) | ||
else | ||
Hmac.verify(algorithm, key, signing_input, signature) | ||
end | ||
rescue ::RbNaCl::BadAuthenticatorError | ||
false | ||
end | ||
|
||
def key_for_rbnacl(hmac, key) | ||
key ||= '' | ||
raise JWT::DecodeError, 'HMAC key expected to be a String' unless key.is_a?(String) | ||
|
||
return padded_empty_key(hmac.key_bytes) if key == '' | ||
|
||
key | ||
end | ||
|
||
def resolve_algorithm(algorithm) | ||
MAPPING.fetch(algorithm) | ||
end | ||
|
||
def padded_empty_key(length) | ||
Array.new(length, 0x0).pack('C*').encode('binary') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module Algos | ||
module HmacRbNaClFixed | ||
module_function | ||
|
||
MAPPING = { | ||
'HS256' => ::RbNaCl::HMAC::SHA256, | ||
'HS512256' => ::RbNaCl::HMAC::SHA512256, | ||
'HS384' => nil, | ||
'HS512' => ::RbNaCl::HMAC::SHA512 | ||
}.freeze | ||
|
||
SUPPORTED = MAPPING.keys | ||
|
||
def sign(algorithm, msg, key) | ||
key ||= '' | ||
|
||
raise JWT::DecodeError, 'HMAC key expected to be a String' unless key.is_a?(String) | ||
|
||
if (hmac = resolve_algorithm(algorithm)) && key.bytesize <= hmac.key_bytes | ||
hmac.auth(padded_key_bytes(key, hmac.key_bytes), msg.encode('binary')) | ||
else | ||
Hmac.sign(algorithm, msg, key) | ||
end | ||
end | ||
|
||
def verify(algorithm, key, signing_input, signature) | ||
key ||= '' | ||
|
||
raise JWT::DecodeError, 'HMAC key expected to be a String' unless key.is_a?(String) | ||
|
||
if (hmac = resolve_algorithm(algorithm)) && key.bytesize <= hmac.key_bytes | ||
hmac.verify(padded_key_bytes(key, hmac.key_bytes), signature.encode('binary'), signing_input.encode('binary')) | ||
else | ||
Hmac.verify(algorithm, key, signing_input, signature) | ||
end | ||
rescue ::RbNaCl::BadAuthenticatorError | ||
false | ||
end | ||
|
||
def resolve_algorithm(algorithm) | ||
MAPPING.fetch(algorithm) | ||
end | ||
|
||
def padded_key_bytes(key, bytesize) | ||
key.bytes.fill(0, key.bytesize...bytesize).pack('C*') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.