-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Cryptographic-API-Services/pre-release
Pre release
- Loading branch information
Showing
9 changed files
with
135 additions
and
2 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
Binary file not shown.
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,49 @@ | ||
import { hmacSign, hmacSignThreadpool, hmacVerify, hmacVerifyThreadpool } from "../../index"; | ||
|
||
export class HmacWrapper { | ||
public hmacSignBytes(key: Array<number>, message: Array<number>): Array<number> { | ||
if (key?.length === 0) { | ||
throw new Error("Must provide an allocated key"); | ||
} | ||
if (message?.length === 0) { | ||
throw new Error("Must provide an allocated message"); | ||
} | ||
return hmacSign(key, message); | ||
} | ||
|
||
public hmacVerifyBytes(key: Array<number>, message: Array<number>, signature: Array<number>): boolean { | ||
if (key?.length === 0) { | ||
throw new Error("Must provide an allocated key"); | ||
} | ||
if (message?.length === 0) { | ||
throw new Error("Must provide an allocated message"); | ||
} | ||
if(signature?.length===0) { | ||
throw new Error("Must provide an allocated signature"); | ||
} | ||
return hmacVerify(key, message, signature); | ||
} | ||
|
||
public hmacSignBytesThreadpool(key: Array<number>, message: Array<number>): Array<number> { | ||
if (key?.length === 0) { | ||
throw new Error("Must provide an allocated key"); | ||
} | ||
if (message?.length === 0) { | ||
throw new Error("Must provide an allocated message"); | ||
} | ||
return hmacSignThreadpool(key, message); | ||
} | ||
|
||
public hmacVerifyBytesThreadpool(key: Array<number>, message: Array<number>, signature: Array<number>): boolean { | ||
if (key?.length === 0) { | ||
throw new Error("Must provide an allocated key"); | ||
} | ||
if (message?.length === 0) { | ||
throw new Error("Must provide an allocated message"); | ||
} | ||
if(signature?.length===0) { | ||
throw new Error("Must provide an allocated signature"); | ||
} | ||
return hmacVerifyThreadpool(key, message, signature); | ||
} | ||
} |
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,3 @@ | ||
import { HmacWrapper } from "./hmac"; | ||
|
||
export { HmacWrapper }; |
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 |
---|---|---|
|
@@ -32,4 +32,8 @@ mod digital_signature { | |
|
||
mod sponges { | ||
pub mod ascon_aead; | ||
} | ||
|
||
mod message { | ||
pub mod hmac; | ||
} |
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,40 @@ | ||
use cas_lib::message::{cas_hmac::CASHMAC, hmac::HMAC}; | ||
use napi_derive::napi; | ||
|
||
#[napi] | ||
pub fn hmac_sign(key: Vec<u8>, message: Vec<u8>) -> Vec<u8> { | ||
return <HMAC as CASHMAC>::sign(key, message); | ||
} | ||
|
||
#[napi] | ||
pub fn hmac_sign_threadpool(key: Vec<u8>, message: Vec<u8>) -> Vec<u8> { | ||
return <HMAC as CASHMAC>::sign_threadpool(key, message); | ||
} | ||
|
||
#[napi] | ||
pub fn hmac_verify(key: Vec<u8>, message: Vec<u8>, signature: Vec<u8>) -> bool { | ||
return <HMAC as CASHMAC>::verify(key, message, signature); | ||
} | ||
|
||
#[napi] | ||
pub fn hmac_verify_threadpool(key: Vec<u8>, message: Vec<u8>, signature: Vec<u8>) -> bool { | ||
return <HMAC as CASHMAC>::verify_threadpool(key, message, signature); | ||
} | ||
|
||
#[test] | ||
fn hmac_sign_and_verify_test() { | ||
let mut key = b"ThisIsMyKeyForHmac".to_vec(); | ||
let mut message = b"ThisIsMyMessageToSign".to_vec(); | ||
let signature = hmac_sign(key.clone(), message.clone()); | ||
let result = hmac_verify(key, message, signature); | ||
assert_eq!(true, result); | ||
} | ||
|
||
#[test] | ||
fn hmac_sign_and_verify_threadpool_test() { | ||
let mut key = b"ThisIsMyKeyForHmac7789".to_vec(); | ||
let mut message = b"ThisIsMyMessageToSign1230".to_vec(); | ||
let signature = hmac_sign_threadpool(key.clone(), message.clone()); | ||
let result = hmac_verify_threadpool(key, message, signature); | ||
assert_eq!(true, result); | ||
} |
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,32 @@ | ||
import { assert } from "chai"; | ||
import { HmacWrapper } from "../src-ts/message/index"; | ||
|
||
describe("HMAC Tests", () => { | ||
it("Sign and Verify", () => { | ||
const wrapper = new HmacWrapper(); | ||
const key: string = "This is my array to hash"; | ||
const encoder = new TextEncoder(); | ||
const keyBytes: Array<number> = Array.from(encoder.encode(key)); | ||
const message: string = "This is my message"; | ||
const messageBytes = Array.from(encoder.encode(message)); | ||
const signature = wrapper.hmacSignBytes(keyBytes, messageBytes); | ||
const result = wrapper.hmacVerifyBytes(keyBytes, messageBytes, signature); | ||
assert.equal(true, result); | ||
}); | ||
|
||
it("Sign and Verify Threadpool", () => { | ||
const wrapper = new HmacWrapper(); | ||
const key: string = "This is my array to hash"; | ||
const encoder = new TextEncoder(); | ||
const keyBytes: Array<number> = Array.from(encoder.encode(key)); | ||
const message: string = "This is my message"; | ||
const messageBytes = Array.from(encoder.encode(message)); | ||
const signature = wrapper.hmacSignBytesThreadpool(keyBytes, messageBytes); | ||
const result = wrapper.hmacVerifyBytesThreadpool( | ||
keyBytes, | ||
messageBytes, | ||
signature, | ||
); | ||
assert.equal(true, result); | ||
}); | ||
}); |