ndn-js
Version:
A JavaScript client library for Named Data Networking
106 lines (91 loc) • 4.02 kB
HTML
<!--
* Copyright (C) 2016-2019 Regents of the University of California.
* @author: Jeff Thompson <jefft0@remap.ucla.edu>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* A copy of the GNU Lesser General Public License is in the file COPYING.
-->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>NDN Sign/Verify a Data Packet with HMAC</title>
<script type="text/javascript" src="../../build/ndn.js"></script>
<script type="text/javascript">
var TlvData = new Blob(new Buffer([
0x06, 0x49, // NDN Data
0x07, 0x0a, // Name
0x08, 0x03, 0x6e, 0x64, 0x6e, // "ndn"
0x08, 0x03, 0x61, 0x62, 0x63, // "abc"
0x14, 0x00, // MetaInfo
0x15, 0x08, 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21, // Content = "SUCCESS!"
0x16, 0x0d, // SignatureInfo
0x1b, 0x01, 0x04, // SignatureType = SignatureHmacWithSha256
0x1c, 0x08, // KeyLocator
0x07, 0x06, // Name
0x08, 0x04, 0x6b, 0x65, 0x79, 0x31, // "key1"
0x17, 0x20, // SignatureValue
0x19, 0x86, 0x8e, 0x71, 0x83, 0x99, 0x8d, 0xf3, 0x73, 0x33,
0x2f, 0x3d, 0xd1, 0xc9, 0xc9, 0x50, 0xfc, 0x29, 0xd7, 0x34,
0xc0, 0x79, 0x77, 0x79, 0x1d, 0x83, 0x96, 0xfa, 0x3b, 0x91,
0xfd, 0x36
]), false);
// Use a hard-wired secret for testing. In a real application the signer
// ensures that the verifier knows the shared key and its keyName.
var HmacKey = new Blob(new Buffer([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
]), false);
function encode()
{
var name = new Name(document.getElementById('contentname').value);
var content = document.getElementById('content').value;
var freshData = new Data(name);
var signature = new HmacWithSha256Signature();
signature.getKeyLocator().setType(KeyLocatorType.KEYNAME);
signature.getKeyLocator().setKeyName(new Name("key1"));
freshData.setSignature(signature);
freshData.setContent(content);
KeyChain.signWithHmacWithSha256(freshData, HmacKey);
var output = EncodingUtils.encodeToHexData(freshData);
document.getElementById('result').innerHTML = output;
}
function decode()
{
var input = document.getElementById('result').innerHTML.toUpperCase();
var data = EncodingUtils.decodeHexData(input);
var output = EncodingUtils.dataToHtml(data);
// Verify with the same KeyChain used to sign. Set the document
// element inside the callback so that it receives the result.
if (KeyChain.verifyDataWithHmacWithSha256(data, HmacKey))
output += "<br/>Freshly-signed data signature verification: VERIFIED<br/>";
else
output += "<br/>Freshly-signed data signature verification: FAILED<br/>";
document.getElementById('result').innerHTML = output;
}
</script>
</head>
<body >
<form>
Please enter a Data packet name:<br />
<input id="contentname" type="text" name="CONTENTNAME" value="/ndn/abc" />
<br />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>