Created
August 1, 2018 09:34
-
-
Save tom-field/6b8a0c33542c21249af302c9a998417c to your computer and use it in GitHub Desktop.
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
<html> | |
<head> | |
<title>Sample Application for RSA signing in JavaScript</title> | |
<script language="JavaScript" type="text/javascript" src="../jsrsasign-all-min.js"></script> | |
<script language="JavaScript" type="text/javascript"> | |
//----------第一种方法 | |
/*function doSign() { | |
//获取私钥 | |
var rsa = KEYUTIL.getKey(document.form1.prvkey1.value); | |
//获取算法 | |
var hashAlg = document.form1.hashalg.value; | |
//数据转二进制加签 返回十六进制 | |
var hSig = rsa.sign(stoBA(document.form1.msgsigned.value), hashAlg); | |
//对加签数据转base64 | |
document.form1.siggenerated.value = linebrk(hextob64(hSig), 64); | |
}*/ | |
//----------第二种方法 | |
function doSign() { | |
// 获取私钥 | |
var rsa = KEYUTIL.getKey(document.form1.prvkey1.value); | |
// 创建Signature对象,设置签名编码算法 | |
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"}); | |
// 初始化 | |
sig.init(rsa); | |
// 传入待加签字符串 | |
sig.updateString(document.form1.msgsigned.value); | |
// 生成密文 | |
var hSig = hextob64(sig.sign()); | |
document.form1.siggenerated.value = linebrk(hSig, 64); | |
} | |
// 第一种验签名 | |
/*function doVerify() { | |
var sMsg = document.form1.msgverified.value; | |
var hSig = document.form1.sigverified.value; | |
var pubKey = KEYUTIL.getKey(document.form1.cert.value); | |
var isValid = pubKey.verify(stoBA(sMsg), b64tohex(hSig)); | |
// display verification result | |
if (isValid) { | |
_displayStatus("valid"); | |
} else { | |
_displayStatus("invalid"); | |
} | |
}*/ | |
// 第二种验证签 | |
function doVerify() { | |
var sMsg = document.form1.msgverified.value; | |
var hSig = document.form1.sigverified.value; | |
var publicKey = document.form1.cert.value; | |
//初始化签名对象 | |
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"}); | |
//初始化 | |
sig.init(publicKey); | |
// 传入待解签字符串 | |
sig.updateString(sMsg); | |
var isValid = sig.verify(b64tohex(hSig)); | |
if (isValid) { | |
_displayStatus("valid"); | |
} else { | |
_displayStatus("invalid"); | |
} | |
} | |
function copyMsgAndSig() { | |
_displayStatus("reset"); | |
document.form1.msgverified.value = document.form1.msgsigned.value; | |
document.form1.sigverified.value = document.form1.siggenerated.value; | |
} | |
function _displayStatus(sStatus) { | |
var div1 = document.getElementById("verifyresult"); | |
if (sStatus == "valid") { | |
div1.style.backgroundColor = "skyblue"; | |
div1.innerHTML = "This signature is *VALID*."; | |
} else if (sStatus == "invalid") { | |
div1.style.backgroundColor = "deeppink"; | |
div1.innerHTML = "This signature is *NOT VALID*."; | |
} else { | |
div1.style.backgroundColor = "yellow"; | |
div1.innerHTML = "Please fill values below and push [Verify this sigunature] button."; | |
} | |
} | |
</script> | |
<style type="text/css"> | |
TD { | |
vertical-align: top | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Sample Application for RSA signing in JavaScript</h1> | |
<form name="form1"> | |
<table border="0"> | |
<tr> | |
<th>Signer</th> | |
<th></th> | |
<th>Verifier</th> | |
</tr> | |
<tr> | |
<td> | |
PEM RSA Private Key<br/> | |
<!-- _test/z5.* for X.509v1 certificate and private key --> | |
<textarea name="prvkey1" rows="10" cols="65">-----BEGIN PRIVATE KEY----- | |
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ3rUCXGLmADCxQfLEjrnf0EZ4sMsBF5ToGS7wxCAoV0jEK6x3Ne0wr7QplJu0ppIq73dXIjCn/dd6n3aoK+8rl6aIx20CD8qvLnP6QJKCY5S5OzZRkgvKvCQ8ofq2LX5LAOIkSUpXOoxh2JgouiOrcMkaRWBmlEbrrjvp2IoNslAgMBAAECgYBwrbiQW2o3o7RB9UQ5+7TNFllzQ+hVb4LuvZLH4km+Q7zY2ED8NwrK6SFKEuwKWh4fTf6FJC+XsMRziFB9XhX91W4XZkIr7i/BYfSDdhvNy/hEqNyVNtC+N22jJuv9P/JSb/uESuBNpN2p7pUaJu9Lo3UOhPgOpdOYAzr/DbNGeQJBAOEn7LKHHaYaTaB9nfNhlLdJtalXsOrBEyRzWkhfBXSckHpQbE5LZOQ2h+8imGZu14OFy1/DPyCf3dKYzuxD3f8CQQCzjXArVESEFP1A8cw3Qwx9Kt49uaKpBlp6xyu5TXVxeVvy6Z0daPTd7DWGDwK57r4okf7bRQ+WyzUal1jnkg7bAkAhDpGL7JaHypjXFVpZX1xMRMAtKrjI3qwG0ADTW1AbvNjhfGlKXXtknjT7ASNK9r8hikU/vBTKn24b+192BMjVAkBvw4igz/FeLhp+RISTmX66ouZ3kYe4oqXLaxLCY37JRXuAtv3/god5ZEDk048k4YEoi8gSAOj1OkQx6yeB7Z6lAkEAsAe+eMz7aS3nLpIe/m1u9fsGlLmyC51U/LLJ5ENsfT/E/A97T73xrQ6mcGu2/W67/v2uuyJ8aZhUUcIcqj0TEQ== | |
-----END PRIVATE KEY-----</textarea><br/> | |
Text message to be signed.<br/> | |
<input type="text" name="msgsigned" size="100" | |
value='{"calType":"","pageNo":"1","pageSize":"20","remark":"测试","target":""}'/><br/> | |
</td> | |
<td></td> | |
<td> | |
Verification Result | |
<div id="verifyresult" style="background: yellow">Please fill values below and push "Verify this | |
sigunature" button. | |
</div> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<select name="hashalg"> | |
<option value="sha1" selected>SHA1 | |
<option value="sha256">SHA256 | |
<option value="sha512">SHA512 | |
<option value="md5">MD5 | |
<option value="ripemd160">RIPEMD-160 | |
</select> | |
<input type="button" value="Sign to this message ↓" onClick="doSign();"/><br/> | |
</td> | |
<td> | |
<input type="button" value="Copy →" onClick="copyMsgAndSig();"/><br/> | |
</td> | |
<td> | |
<input type="button" value="Verify this signature ↑" onClick="doVerify();"/><br/> | |
</td> | |
</tr> | |
<tr> | |
<td> | |
Generated Signature<br/> | |
<textarea name="siggenerated" rows="4" cols="65"></textarea> | |
</td> | |
<td> | |
</td> | |
<td> | |
Verifying Signature<br/> | |
<textarea name="sigverified" rows="4" cols="65"> | |
6f7df91d8f973a0619d525c319337741130b77b21f9667dc7d1d74853b644cbe | |
5e6b0e84aacc2faee883d43affb811fc653b67c38203d4f206d1b838c4714b6b | |
2cf17cd621303c21bac96090df3883e58784a0576e501c10cdefb12b6bf887e5 | |
48f6b07b09ae80d8416151d7dab7066d645e2eee57ac5f7af2a70ee0724c8e47 | |
</textarea><br/> | |
Text message to be verified.<br/> | |
<input type="text" name="msgverified" size="100" | |
value='{"calType":"","pageNo":"1","pageSize":"20","remark":"测试","target":""}'/><br/> | |
Signer's Public Key Certificate.<br/> | |
<textarea name="cert" rows="10" cols="65"> | |
-----BEGIN PUBLIC KEY----- | |
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCd61Alxi5gAwsUHyxI6539BGeLDLAReU6Bku8MQgKFdIxCusdzXtMK+0KZSbtKaSKu93VyIwp/3Xep92qCvvK5emiMdtAg/Kry5z+kCSgmOUuTs2UZILyrwkPKH6ti1+SwDiJElKVzqMYdiYKLojq3DJGkVgZpRG66476diKDbJQIDAQAB | |
-----END PUBLIC KEY----- | |
</textarea><br/> | |
</td> | |
</tr> | |
</table> | |
</form> | |
<h3>How to sign and verify a text message (simple usage)</h3> | |
<ol> | |
<li>Modify the text message 'aaa' to anything you want.</li> | |
<li>Choose hash algorithm for signing 'SHA1' or 'SHA256'.</li> | |
<li>Push 'Sign to this message' button in the left.</li> | |
<li>Push 'Copy' button in the middle to copy the message to be signed and the generated signature value to | |
verification form in the right. | |
</li> | |
<li>Push 'Verify this message' in the right.</li> | |
<li>Then you can see signature verification result in the top of right.</li> | |
</ol> | |
<h3>Note for signing in the left form.</h3> | |
See below when you want to specify message and private key to be signed. | |
<ul> | |
<li>In the 'PEM RSA Private Key' text area, you can specify | |
signer's private key. The format of the key should be | |
PKCS#1 PEM text formatted and unencrypted RSA private key. | |
</li> | |
</ul> | |
<h3>Note for signature verification in the right form.</h3> | |
See below when you want to specify message, | |
signature value and public key certificate to be verified. | |
<ul> | |
<li>In the 'Verifying Signature' field, you can specify | |
any signature value to be verified. | |
Signature value should be hexa decimal encoded 'RSASSA-PKCS1-v1_5' signature. | |
Currently this supports 'SHA1withRSA' and 'SHA256withRSA' signature algorihtm. | |
RSA key length have been tested from 512bit to 2048bit for this program. | |
</li> | |
<li> | |
In the "Signer's Public Key Certificate" field, | |
you can specify signer's public key certificate to be verified. | |
The value should be PEM encoded X.509 certificate with RSA public key. | |
X.509v1 and X.509v3 is available however X.509v2 is not supported now. | |
</li> | |
</ul> | |
<center> | |
Copyright © 2010-2012 Kenji Urushima, All Rights Reserved. | |
</center> | |
<div align="right"> | |
<a href="index.html">Go back to index</a> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment