Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,99 @@
|
||||
"""同一配置密钥和动态消息的 HMAC-SHA256 → SHA256 → 全摘要校验和;独立成熟库对端。"""
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
|
||||
|
||||
def config():
|
||||
return {'module': {'main': {'main': {'key': 'public-benchmark-key'}}}}
|
||||
|
||||
|
||||
def expected(n):
|
||||
key = config()['module']['main']['main']['key'].encode()
|
||||
values = [sum(hashlib.sha256(hmac.digest(key, str(index).encode(), 'sha256')).digest()) for index in range(1000)]
|
||||
return sum(values) * (n // 1000) + sum(values[:n % 1000])
|
||||
|
||||
|
||||
def source(language):
|
||||
if language == 'dever':
|
||||
return '''setting key: Secret
|
||||
loop checksum(value: Bytes) (out: Int)
|
||||
| each byte in value | result |
|
||||
| out | sum byte |
|
||||
roundtrip(value: Int) (out: Int) {
|
||||
message = dever.bytes.fromText(int.toText(value % 1000))
|
||||
signed = dever.crypto.hmacSha256(key, message)
|
||||
out = checksum(dever.crypto.sha256(signed))
|
||||
}
|
||||
loop measure(n: Int) (out: Int)
|
||||
| each value in range(0, n) | result |
|
||||
| out | sum roundtrip(value) |
|
||||
cmd bench = app.measure
|
||||
'''
|
||||
if language == 'rust':
|
||||
return '''use serde::Deserialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use hmac::{Hmac, Mac};
|
||||
use bytes::Bytes;
|
||||
use zeroize::Zeroizing;
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Root { module: Module }
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Module { main: Domain }
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Domain { main: Settings }
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Settings { key: String }
|
||||
fn sign(key: &[u8], message: &[u8]) -> Bytes {
|
||||
let mut mac = Hmac::<Sha256>::new_from_slice(key).unwrap();
|
||||
mac.update(message);
|
||||
Bytes::copy_from_slice(&mac.finalize().into_bytes())
|
||||
}
|
||||
fn hash(message: &[u8]) -> Bytes { Bytes::copy_from_slice(&Sha256::digest(message)) }
|
||||
fn main() {
|
||||
let path = std::env::args().nth(2).unwrap();
|
||||
let input = Zeroizing::new(std::fs::read_to_string(path).unwrap());
|
||||
let root: Root = serde_json::from_str(&input).unwrap();
|
||||
let key = Zeroizing::new(root.module.main.main.key.into_bytes());
|
||||
let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap();
|
||||
let mut total = 0i64;
|
||||
for value in 0..n {
|
||||
let message = Bytes::from((value % 1000).to_string());
|
||||
let signed = sign(&key, &message);
|
||||
let digest = hash(&signed);
|
||||
let mut checksum = 0i64;
|
||||
for byte in digest { checksum = checksum.checked_add(i64::from(byte)).unwrap(); }
|
||||
total = total.checked_add(checksum).unwrap();
|
||||
}
|
||||
println!("{total}");
|
||||
}
|
||||
'''
|
||||
return '''package main
|
||||
import ("crypto/hmac"; "crypto/sha256"; "encoding/json"; "fmt"; "os"; "strconv"; "strings")
|
||||
type Settings struct { Key string }
|
||||
type Domain struct { Main Settings }
|
||||
type Module struct { Main Domain }
|
||||
type Root struct { Module Module }
|
||||
func main() {
|
||||
input, err := os.ReadFile(os.Args[2]); if err != nil { panic(err) }
|
||||
var root Root
|
||||
decoder := json.NewDecoder(strings.NewReader(string(input))); decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&root); err != nil { panic(err) }
|
||||
n, err := strconv.ParseInt(os.Args[1], 10, 64); if err != nil { panic(err) }
|
||||
key := []byte(root.Module.Main.Main.Key)
|
||||
total := int64(0)
|
||||
for value := int64(0); value < n; value++ {
|
||||
message := []byte(strconv.FormatInt(value % 1000, 10))
|
||||
mac := hmac.New(sha256.New, key); mac.Write(message)
|
||||
digest := sha256.Sum256(mac.Sum(nil))
|
||||
checksum := int64(0)
|
||||
for _, value := range digest { checksum += int64(value) }
|
||||
next := total + checksum; if next < total { panic("integer overflow") }; total = next
|
||||
}
|
||||
fmt.Println(total)
|
||||
}
|
||||
'''
|
||||
Reference in new issue
Block a user