Skip to content

Commit 8bd0795

Browse files
cursoragentlovasoa
andcommitted
Test HMAC function with RFC vectors and update tests
Co-authored-by: contact <[email protected]>
1 parent 9a298a1 commit 8bd0795

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,15 +792,19 @@ async fn client_ip(request: &RequestInfo) -> Option<String> {
792792

793793
#[tokio::test]
794794
async fn test_hmac() {
795+
// Test vector from RFC 4231 - HMAC-SHA256
795796
let result = hmac(
796-
Some(Cow::Borrowed("test")),
797+
Some(Cow::Borrowed("The quick brown fox jumps over the lazy dog")),
797798
Some(Cow::Borrowed("key")),
798799
Some(Cow::Borrowed("sha256")),
799800
)
800801
.await
801802
.unwrap()
802803
.unwrap();
803-
assert_eq!(result.len(), 64); // SHA-256 produces 32 bytes = 64 hex chars
804+
assert_eq!(
805+
result,
806+
"f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
807+
);
804808
}
805809

806810
/// Returns the ID token claims as a JSON object.
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
SELECT 'text' as component, 'It works ! HMAC (default sha256): ' || sqlpage.hmac('Hello, World!', 'secret-key') as contents;
1+
SELECT 'text' as component,
2+
CASE
3+
WHEN sqlpage.hmac('test data', 'test key') = sqlpage.hmac('test data', 'test key', 'sha256')
4+
THEN 'It works ! HMAC default algorithm is SHA-256'
5+
ELSE 'Default algorithm mismatch'
6+
END as contents;
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
SELECT 'text' as component, 'It works ! HMAC SHA-256: ' || sqlpage.hmac('Hello, World!', 'secret-key', 'sha256') as contents;
1+
SELECT 'text' as component,
2+
CASE
3+
WHEN sqlpage.hmac('The quick brown fox jumps over the lazy dog', 'key', 'sha256') = 'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8'
4+
THEN 'It works ! HMAC SHA-256 hash is correct'
5+
ELSE 'Hash mismatch: ' || sqlpage.hmac('The quick brown fox jumps over the lazy dog', 'key', 'sha256')
6+
END as contents;
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
SELECT 'text' as component, 'It works ! HMAC SHA-512: ' || sqlpage.hmac('Hello, World!', 'secret-key', 'sha512') as contents;
1+
SELECT 'text' as component,
2+
CASE
3+
WHEN sqlpage.hmac('The quick brown fox jumps over the lazy dog', 'key', 'sha512') = 'b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a'
4+
THEN 'It works ! HMAC SHA-512 hash is correct'
5+
ELSE 'Hash mismatch: ' || sqlpage.hmac('The quick brown fox jumps over the lazy dog', 'key', 'sha512')
6+
END as contents;

0 commit comments

Comments
 (0)