blob: df3e7005627c655d73301c959cfc15ea8ada98da [file] [log] [blame]
<?xml version = "1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>NDN Request Example</title>
<script type="text/javascript" src="../Helper.js"></script>
<script type="text/javascript">
function encode(){
var contentname = new Name( Name.createNameArray(document.getElementById('contentname').value) );
var content = document.getElementById('content').value;
var signedInfo = new SignedInfo();
signedInfo.setFields();
//var signatureBits = generateSignatureBits(contentname,content,signedInfo);
//witness is null
var signature = new Signature();
var co = new ContentObject(contentname,signedInfo,content,signature);
co.sign();
var output = encodeToHexContentObject(co);
document.getElementById('result').innerHTML = output;
}
function decode(){
var input = document.getElementById('result').innerHTML;
var co = decodeHexContentObject(input);
if(LOG>3)console.log('CONTENT OBJECT DECODED');
if(LOG>3)console.log(co);
///////////////////////////////////////
var output ="";
if(co==-1)
output+= "NO CONTENT FOUND"
else if (co==-2)
output+= "CONTENT NAME IS EMPTY"
else{
if(co.name!=null && co.name.components!=null){
output+= "NAME: ";
for(var i=0;i<co.name.components.length;i++){
output+= "/"+ toString(co.name.components[i]);
}
output+= "<br />";
output+= "<br />";
}
if(co.content !=null){
output += "CONTENT(ASCII): "+ toString(co.content);
output+= "<br />";
output+= "<br />";
}
if(co.content !=null){
output += "CONTENT(hex): "+ DataUtils.toHex(co.content);
output+= "<br />";
output+= "<br />";
}
if(co.signature !=null && co.signature.signature!=null){
output += "SIGNATURE(hex): "+ DataUtils.toHex(co.signature.signature);
output+= "<br />";
output+= "<br />";
}
if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){
output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest);
output+= "<br />";
output+= "<br />";
}
if(co.signedInfo !=null && co.signedInfo.timestamp!=null){
output += "TimeStamp(hex): "+ co.signedInfo.timestamp.date;
output+= "<br />";
output+= "<br />";
}
if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){
var publickey = rstr2b64(toString(co.signedInfo.locator.publicKey));
var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase();
var publickeyString = toString(co.signedInfo.locator.publicKey);
var signature = DataUtils.toHex(co.signature.signature).toLowerCase();
var input = toString(co.rawSignatureData);
output += "DER Certificate: "+publickey ;
output+= "<br />";
output+= "<br />";
if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input);
if(LOG>2) console.log(" PublicKey = "+publickey );
if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex );
if(LOG>2) console.log(" PublicKeyString = "+publickeyString );
if(LOG>2) console.log(" Signature "+signature );
if(LOG>2) console.log(" Signature NOW IS" );
if(LOG>2) console.log(co.signature.signature);
var x509 = new X509();
x509.readCertPEM(publickey);
//x509.readCertPEMWithoutRSAInit(publickey);
var result = x509.subjectPublicKeyRSA.verifyString(input, signature);
console.log('result is '+result);
/*var rsakey = new RSAKey();
var kp = publickeyHex.slice(56,314);
output += "PUBLISHER KEY(hex): "+kp ;
output+= "<br />";
output+= "<br />";
console.log('kp is '+kp);
var exp = publickeyHex.slice(318,324);
console.log('kp size is '+kp.length );
output += "exponent: "+exp ;
output+= "<br />";
output+= "<br />";
console.log('exp is '+exp);
rsakey.setPublic(kp,exp);
var result = rsakey.verifyString(input, signature);*/
if(result)
output += 'SIGNATURE VALID';
else
output += 'SIGNATURE INVALID';
//output += "VALID: "+ toHex(co.signedInfo.locator.publicKey);
output+= "<br />";
output+= "<br />";
if(LOG>4) console.log('str'[1]);
}
}
document.getElementById('result').innerHTML = output;
}
</script>
</head>
<body >
<form>
Please Enter a Content Name:<br />
<input id="contentname" type="text" name="CONTENTNAME" value="/PARC/abc" />
Please Enter the Content:<br />
<textarea id="content" cols="40" rows="5" name="CONTENT" value="SUCCESS" >SUCCESS!</textarea>
</form>
<button onclick="encode()">Encode</button>
<button onclick="decode()">Decode</button>
<p id="result"></p>
</body>
</html>