mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 08:56:17 +08:00
49 lines
1.1 KiB
HTML
49 lines
1.1 KiB
HTML
<html>
|
|
<script src="sm3.js"></script>
|
|
<script src="sm4.js"></script>
|
|
<body>
|
|
<h1>GmSSL JavaScript</h1>
|
|
<script>
|
|
var m = [0x61, 0x62, 0x63];
|
|
var dgst = sm3(m);
|
|
console.log("sm3(\"abc\") = " + dgst);
|
|
|
|
var hmac_key = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36];
|
|
var hmac = [
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
];
|
|
sm3_hmac(m, hmac_key, hmac);
|
|
console.log("sm3_hmac(\"abc\", \"123456\") = " + hmac);
|
|
|
|
var user_key = [
|
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
|
|
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
|
|
];
|
|
|
|
var plaintext = [
|
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
|
|
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
|
|
];
|
|
|
|
var key = sm4_key_new();
|
|
var ciphertext = new Array(SM4_BLOCK_SIZE);
|
|
|
|
sm4_set_encrypt_key(key, user_key);
|
|
sm4_encrypt(plaintext, 0, ciphertext, 0, key);
|
|
|
|
console.log("sm4 ciphertext = " + ciphertext);
|
|
|
|
sm4_set_decrypt_key(key, user_key);
|
|
sm4_decrypt(ciphertext, 0, plaintext, 0, key);
|
|
console.log("sm4 plaintext = " + plaintext);
|
|
|
|
|
|
sm4_key_free(key);
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|