Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre release #37

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function ascon128KeyGenerate(): Array<number>
export function ascon128NonceGenerate(): Array<number>
export function ascon128Encrypt(key: Array<number>, nonce: Array<number>, plaintext: Array<number>): Array<number>
export function ascon128Decrypt(key: Array<number>, nonce: Array<number>, ciphertext: Array<number>): Array<number>
export function hmacSign(key: Array<number>, message: Array<number>): Array<number>
export function hmacSignThreadpool(key: Array<number>, message: Array<number>): Array<number>
export function hmacVerify(key: Array<number>, message: Array<number>, signature: Array<number>): boolean
export function hmacVerifyThreadpool(key: Array<number>, message: Array<number>, signature: Array<number>): boolean
export type CASx25519SecretPublicKeyResult = CaSx25519SecretPublicKeyResult
export class CaSx25519SecretPublicKeyResult {
publicKey: Array<number>
Expand Down
Binary file modified index.node
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{
"name": "cas-typescript-sdk",
"version": "1.0.25",
"version": "1.0.26",
"description": "",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from "./symmetric/index";
export * from "./asymmetric/index";
export * from "./hybrid/index";
export * from "./digital-signature";
export * from "./sponges/index";
export * from "./sponges/index";
export * from "./message/index";
49 changes: 49 additions & 0 deletions src-ts/message/hmac.ts
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);
}
}
3 changes: 3 additions & 0 deletions src-ts/message/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { HmacWrapper } from "./hmac";

export { HmacWrapper };
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ mod digital_signature {

mod sponges {
pub mod ascon_aead;
}

mod message {
pub mod hmac;
}
40 changes: 40 additions & 0 deletions src/message/hmac.rs
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);
}
32 changes: 32 additions & 0 deletions test-ts/hmac.test.spec.ts
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);
});
});
Loading