feat: can run qjs

This commit is contained in:
2020-11-07 16:12:21 +08:00
parent 5110732561
commit 56b44d1a8f
60 changed files with 91718 additions and 16 deletions

View File

@@ -3,8 +3,8 @@ use std::io::{ Write, Read };
use serde::{ Deserialize, Serialize };
use ring::{
signature::{ KeyPair, Ed25519KeyPair, UnparsedPublicKey, ED25519 },
hmac, rand, error::Unspecified,
digest,
rand,
// hmac, digest, error::Unspecified,
};
use rust_util::XResult;
@@ -14,7 +14,7 @@ pub struct SigningKeyPair {
}
impl SigningKeyPair {
fn new() -> Self {
pub fn new() -> Self {
let rng = rand::SystemRandom::new();
let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap(); // TODO ...
SigningKeyPair{
@@ -22,19 +22,19 @@ impl SigningKeyPair {
}
}
fn parse(&self) -> Ed25519KeyPair {
pub fn key_pair(&self) -> Ed25519KeyPair {
Ed25519KeyPair::from_pkcs8(&self.key_pair).unwrap() // TODO ...
}
fn public_key(&self) -> Vec<u8> {
self.parse().public_key().as_ref().to_vec()
pub fn public_key(&self) -> Vec<u8> {
self.key_pair().public_key().as_ref().to_vec()
}
fn unparsed_public_key(&self) -> UnparsedPublicKey<Vec<u8>> {
pub fn unparsed_public_key(&self) -> UnparsedPublicKey<Vec<u8>> {
UnparsedPublicKey::new(&ED25519, self.public_key())
}
fn read_from_file(file: &str) -> XResult<Self> {
pub fn read_from_file(file: &str) -> XResult<Self> {
match File::open(file) {
Err(e) => Err(rust_util::new_box_ioerror(&format!("Read from file failed: {}", e))),
Ok(mut f) => {
@@ -53,7 +53,7 @@ impl SigningKeyPair {
}
}
fn write_to_file(&self, file: &str) -> XResult<()> {
pub fn write_to_file(&self, file: &str) -> XResult<()> {
if File::open(file).is_ok() {
return Err(rust_util::new_box_ioerror(&format!("File exists: {}", file)));
}
@@ -90,7 +90,7 @@ impl SignedMessage {
}
pub fn sign(&mut self, key_pair: &SigningKeyPair) {
let sig = key_pair.parse().sign(&self.msg);
let sig = key_pair.key_pair().sign(&self.msg);
self.sig = Some(sig.as_ref().to_vec());
}
@@ -113,4 +113,4 @@ fn test_sign() {
);
signed_message.sign(&signing_key_pair);
assert!(signed_message.verify(&signing_key_pair.public_key()))
}
}