-
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.
Browse files
Browse the repository at this point in the history
…ital-signature #9 ed25519 digital signature
- Loading branch information
Showing
14 changed files
with
224 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { CASRSADigitalSignatureResult } from "../../index"; | ||
import { RSADigitalSignatureResult, SHAED25519DalekDigitalSignatureResult } from "../../index"; | ||
|
||
export interface IDigitalSignature { | ||
createRsa(rsa_key_size: number, data_to_sign: Array<number>): CASRSADigitalSignatureResult; | ||
createRsa(rsa_key_size: number, data_to_sign: Array<number>): RSADigitalSignatureResult; | ||
verifyRSa(public_key: string, data_to_verify: Array<number>, signature: Array<number>): boolean; | ||
createED25519(dataToSign: Array<number>): SHAED25519DalekDigitalSignatureResult; | ||
verifyED25519(publicKey: Array<number>, dataToVerify: Array<number>, signature: Array<number>): boolean; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,11 @@ | ||
export enum DigitalSignatureType { | ||
SHA512 = 1, | ||
SHA256 = 2 | ||
} | ||
import { DigitalSignatureType } from "./digital-signature-factory"; | ||
import { DigitalSignatureFactory } from "./digital-signature-factory"; | ||
import { DigitalSignatureSHA256Wrapper } from "./digital-signaturte-sha-256"; | ||
import { DigitalSignatureSHA512Wrapper } from "./digital-siganture-sha-512"; | ||
|
||
export { | ||
DigitalSignatureFactory, | ||
DigitalSignatureSHA256Wrapper, | ||
DigitalSignatureSHA512Wrapper, | ||
DigitalSignatureType | ||
}; |
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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
use napi_derive::napi; | ||
|
||
#[napi(constructor)] | ||
pub struct CASRSADigitalSignatureResult { | ||
pub struct RSADigitalSignatureResult { | ||
pub public_key: String, | ||
pub private_key: String, | ||
pub signature: Vec<u8>, | ||
} | ||
|
||
#[napi(constructor)] | ||
pub struct CASSHAED25519DalekDigitalSignatureResult { | ||
pub struct SHAED25519DalekDigitalSignatureResult { | ||
pub public_key: Vec<u8>, | ||
pub signature: Vec<u8> | ||
} | ||
|
||
pub trait CASRSADigitalSignature { | ||
pub trait RSADigitalSignature { | ||
fn digital_signature_rsa( | ||
rsa_key_size: u32, | ||
data_to_sign: Vec<u8>, | ||
) -> CASRSADigitalSignatureResult; | ||
) -> RSADigitalSignatureResult; | ||
fn verify_rsa(public_key: String, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool; | ||
} | ||
|
||
pub trait CASED25519DigitalSignature { | ||
fn digital_signature_ed25519(data_to_sign: Vec<u8>) -> CASSHAED25519DalekDigitalSignatureResult; | ||
pub trait ED25519DigitalSignature { | ||
fn digital_signature_ed25519(data_to_sign: Vec<u8>) -> SHAED25519DalekDigitalSignatureResult; | ||
fn digital_signature_ed25519_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool; | ||
} |
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,69 @@ | ||
use ed25519_dalek::{Keypair, Signature, Signer, Verifier}; | ||
use napi_derive::napi; | ||
use sha3::{Digest, Sha3_256}; | ||
|
||
use super::cas_digital_signature_rsa::{ | ||
ED25519DigitalSignature, SHAED25519DalekDigitalSignatureResult, | ||
}; | ||
|
||
pub struct SHA256ED25519DigitalSignature; | ||
|
||
impl ED25519DigitalSignature for SHA256ED25519DigitalSignature { | ||
fn digital_signature_ed25519(data_to_sign: Vec<u8>) -> SHAED25519DalekDigitalSignatureResult { | ||
let mut hasher = Sha3_256::new(); | ||
hasher.update(data_to_sign); | ||
let sha_hasher_result = hasher.finalize(); | ||
let mut csprng = rand_07::rngs::OsRng {}; | ||
let keypair = ed25519_dalek::Keypair::generate(&mut csprng); | ||
|
||
let signature = keypair.sign(&sha_hasher_result); | ||
let signature_bytes = signature.to_bytes(); | ||
let public_keypair_bytes = keypair.public.to_bytes(); | ||
let result = SHAED25519DalekDigitalSignatureResult { | ||
public_key: public_keypair_bytes.to_vec(), | ||
signature: signature_bytes.to_vec(), | ||
}; | ||
result | ||
} | ||
|
||
fn digital_signature_ed25519_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool { | ||
let mut hasher = Sha3_256::new(); | ||
hasher.update(data_to_verify); | ||
let sha_hasher_result = hasher.finalize(); | ||
|
||
let public_key_parsed = ed25519_dalek::PublicKey::from_bytes(&public_key).unwrap(); | ||
let signature_parsed = Signature::from_bytes(&signature).unwrap(); | ||
return public_key_parsed | ||
.verify(&sha_hasher_result, &signature_parsed) | ||
.is_ok(); | ||
} | ||
} | ||
|
||
#[napi] | ||
pub fn sha_256_ed25519_digital_signature(data_to_sign: Vec<u8>) -> SHAED25519DalekDigitalSignatureResult { | ||
return SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign); | ||
} | ||
|
||
#[napi] | ||
pub fn sha_256_ed25519_digital_signature_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool { | ||
return SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(public_key, data_to_verify, signature) | ||
} | ||
|
||
#[test] | ||
fn sha_256_ed25519_test() { | ||
let key_size: u32 = 1024; | ||
let data_to_sign = b"GetTheseBytes".to_vec(); | ||
let signature_result: SHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()); | ||
let is_verified: bool = SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, data_to_sign, signature_result.signature); | ||
assert_eq!(is_verified, true); | ||
} | ||
|
||
#[test] | ||
fn sha_512_ed25519_test_fail() { | ||
let key_size: u32 = 1024; | ||
let data_to_sign = b"GetTheseBytes".to_vec(); | ||
let signature_result: SHAED25519DalekDigitalSignatureResult = SHA256ED25519DigitalSignature::digital_signature_ed25519(data_to_sign.clone()); | ||
let not_original_data = b"NOtTHoseBytes".to_vec(); | ||
let is_verified: bool = SHA256ED25519DigitalSignature::digital_signature_ed25519_verify(signature_result.public_key, not_original_data, signature_result.signature); | ||
assert_eq!(is_verified, false); | ||
} |
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.