adding testing server
This commit is contained in:
commit
a8802d6bee
16548
websocket_storage/mongoose.c
Normal file
16548
websocket_storage/mongoose.c
Normal file
File diff suppressed because it is too large
Load Diff
6683
websocket_storage/mongoose.h
Normal file
6683
websocket_storage/mongoose.h
Normal file
File diff suppressed because it is too large
Load Diff
240
websocket_storage/test.html
Normal file
240
websocket_storage/test.html
Normal file
@ -0,0 +1,240 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Tweet Tester</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
<script>
|
||||
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
||||
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
|
||||
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
|
||||
|
||||
if (!window.indexedDB) {
|
||||
window.alert("Your browser doesn't support a stable version of IndexedDB.")
|
||||
}
|
||||
|
||||
var contacts = [];
|
||||
var tweeterID = prompt("Enter FloID : ");
|
||||
var selfwebsocket,followWebSocket;
|
||||
//var privKey = prompt("Enter Private Key");
|
||||
/*
|
||||
var encrypt = {
|
||||
|
||||
p: BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16),
|
||||
|
||||
exponent1: function () {
|
||||
return encrypt.p.add(BigInteger.ONE).divide(BigInteger("4"))
|
||||
},
|
||||
|
||||
calculateY: function (x) {
|
||||
let p = this.p;
|
||||
let exp = this.exponent1();
|
||||
// x is x value of public key in BigInteger format without 02 or 03 or 04 prefix
|
||||
return x.modPow(BigInteger("3"), p).add(BigInteger("7")).mod(p).modPow(exp, p)
|
||||
},
|
||||
|
||||
// Insert a compressed public key
|
||||
getUncompressedPublicKey: function (compressedPublicKey) {
|
||||
|
||||
const p = this.p;
|
||||
|
||||
// Fetch x from compressedPublicKey
|
||||
let pubKeyBytes = Crypto.util.hexToBytes(compressedPublicKey);
|
||||
const prefix = pubKeyBytes.shift() // remove prefix
|
||||
let prefix_modulus = prefix % 2;
|
||||
pubKeyBytes.unshift(0) // add prefix 0
|
||||
let x = new BigInteger(pubKeyBytes)
|
||||
let xDecimalValue = x.toString()
|
||||
|
||||
// Fetch y
|
||||
let y = this.calculateY(x);
|
||||
let yDecimalValue = y.toString();
|
||||
|
||||
// verify y value
|
||||
let resultBigInt = y.mod(BigInteger("2"));
|
||||
|
||||
let check = resultBigInt.toString() % 2;
|
||||
|
||||
if (prefix_modulus !== check) {
|
||||
yDecimalValue = y.negate().mod(p).toString();
|
||||
}
|
||||
|
||||
return {
|
||||
x: xDecimalValue,
|
||||
y: yDecimalValue
|
||||
};
|
||||
},
|
||||
|
||||
getSenderPublicKeyString: function () {
|
||||
privateKey = ellipticCurveEncryption.senderRandom();
|
||||
senderPublicKeyString = ellipticCurveEncryption.senderPublicString(privateKey);
|
||||
return {
|
||||
privateKey: privateKey,
|
||||
senderPublicKeyString: senderPublicKeyString
|
||||
}
|
||||
},
|
||||
|
||||
deriveSharedKeySender: function (receiverCompressedPublicKey, senderPrivateKey) {
|
||||
try {
|
||||
let receiverPublicKeyString = this.getUncompressedPublicKey(
|
||||
receiverCompressedPublicKey);
|
||||
var senderDerivedKey = {
|
||||
XValue: "",
|
||||
YValue: ""
|
||||
};
|
||||
senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
|
||||
receiverPublicKeyString.x,
|
||||
receiverPublicKeyString.y, senderPrivateKey);
|
||||
return senderDerivedKey;
|
||||
} catch (error) {
|
||||
return new Error(error);
|
||||
}
|
||||
},
|
||||
|
||||
deriveReceiverSharedKey: function (senderPublicKeyString, receiverPrivateKey) {
|
||||
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
||||
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString,
|
||||
receiverPrivateKey);
|
||||
},
|
||||
|
||||
getReceiverPublicKeyString: function (privateKey) {
|
||||
return ellipticCurveEncryption.receiverPublicString(privateKey);
|
||||
},
|
||||
|
||||
deriveSharedKeyReceiver: function (senderPublicKeyString, receiverPrivateKey) {
|
||||
try {
|
||||
return ellipticCurveEncryption.receiverSharedKeyDerivation(senderPublicKeyString.XValuePublicString,
|
||||
senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
||||
|
||||
} catch (error) {
|
||||
return new Error(error);
|
||||
}
|
||||
},
|
||||
|
||||
encryptMessage: function (data, receiverCompressedPublicKey) {
|
||||
var senderECKeyData = this.getSenderPublicKeyString();
|
||||
var senderDerivedKey = {
|
||||
XValue: "",
|
||||
YValue: ""
|
||||
};
|
||||
var senderPublicKeyString = {};
|
||||
senderDerivedKey = this.deriveSharedKeySender(
|
||||
receiverCompressedPublicKey, senderECKeyData.privateKey);
|
||||
console.log("senderDerivedKey", senderDerivedKey);
|
||||
let senderKey = senderDerivedKey.XValue + senderDerivedKey.YValue;
|
||||
let secret = Crypto.AES.encrypt(data, senderKey);
|
||||
return {
|
||||
secret: secret,
|
||||
senderPublicKeyString: senderECKeyData.senderPublicKeyString
|
||||
};
|
||||
},
|
||||
|
||||
decryptMessage: function (secret, senderPublicKeyString) {
|
||||
var receiverDerivedKey = {
|
||||
XValue: "",
|
||||
YValue: ""
|
||||
};
|
||||
var receiverECKeyData = {};
|
||||
var myPrivateKey = privKey;
|
||||
if (typeof myPrivateKey !== "string") throw new Error("No private key found.");
|
||||
|
||||
let privateKey = this.wifToDecimal(myPrivateKey, true);
|
||||
if (typeof privateKey.privateKeyDecimal !== "string") throw new Error(
|
||||
"Failed to detremine your private key.");
|
||||
receiverECKeyData.privateKey = privateKey.privateKeyDecimal;
|
||||
|
||||
receiverDerivedKey = this.deriveReceiverSharedKey(senderPublicKeyString,
|
||||
receiverECKeyData.privateKey);
|
||||
console.log("receiverDerivedKey", receiverDerivedKey);
|
||||
|
||||
let receiverKey = receiverDerivedKey.XValue + receiverDerivedKey.YValue;
|
||||
let decryptMsg = Crypto.AES.decrypt(secret, receiverKey);
|
||||
return decryptMsg;
|
||||
},
|
||||
|
||||
ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
|
||||
getPubKeyHex: function(privateKeyHex){
|
||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||
if(key.priv == null){
|
||||
alert("Invalid Private key");
|
||||
return;
|
||||
}
|
||||
key.setCompressed(true);
|
||||
var pubkeyHex = key.getPubKeyHex();
|
||||
return pubkeyHex;
|
||||
},
|
||||
getFLOIDfromPubkeyHex: function(pubkeyHex){
|
||||
var key = new Bitcoin.ECKey().setPub(pubkeyHex);
|
||||
var floID = key.getBitcoinAddress();
|
||||
return floID;
|
||||
},
|
||||
sign: function (msg, privateKeyHex) {
|
||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||
key.setCompressed(true);
|
||||
|
||||
var privateKeyArr = key.getBitcoinPrivateKeyByteArray();
|
||||
privateKey = BigInteger.fromByteArrayUnsigned(privateKeyArr);
|
||||
var messageHash = Crypto.SHA256(msg);
|
||||
|
||||
var messageHashBigInteger = new BigInteger(messageHash);
|
||||
var messageSign = Bitcoin.ECDSA.sign(messageHashBigInteger, key.priv);
|
||||
|
||||
var sighex = Crypto.util.bytesToHex(messageSign);
|
||||
return sighex;
|
||||
},
|
||||
verify: function (msg, signatureHex, publicKeyHex) {
|
||||
var msgHash = Crypto.SHA256(msg);
|
||||
var messageHashBigInteger = new BigInteger(msgHash);
|
||||
|
||||
var sigBytes = Crypto.util.hexToBytes(signatureHex);
|
||||
var signature = Bitcoin.ECDSA.parseSig(sigBytes);
|
||||
|
||||
var publicKeyPoint = this.ecparams.getCurve().decodePointHex(publicKeyHex);
|
||||
|
||||
var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger, signature.r, signature.s,
|
||||
publicKeyPoint);
|
||||
return verify;
|
||||
},
|
||||
wifToDecimal: function(pk_wif, isPubKeyCompressed = false) {
|
||||
let pk = Bitcoin.Base58.decode(pk_wif)
|
||||
pk.shift()
|
||||
pk.splice(-4, 4)
|
||||
//If the private key corresponded to a compressed public key, also drop the last byte (it should be 0x01).
|
||||
if (isPubKeyCompressed == true) pk.pop()
|
||||
pk.unshift(0)
|
||||
privateKeyDecimal = BigInteger(pk).toString()
|
||||
privateKeyHex = Crypto.util.bytesToHex(pk)
|
||||
return {
|
||||
privateKeyDecimal: privateKeyDecimal,
|
||||
privateKeyHex: privateKeyHex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
function initselfWebSocket(){
|
||||
selfwebsocket = new WebSocket("ws://"+location.host+"/ws");
|
||||
selfwebsocket.onopen = function(evt){
|
||||
console.log("CONNECTED");
|
||||
var pass = prompt("Enter server password :")
|
||||
selfwebsocket.send("$"+pass);
|
||||
};
|
||||
selfwebsocket.onclose = function(evt){
|
||||
console.log("DISCONNECTED");
|
||||
};
|
||||
selfwebsocket.onmessage = function(evt){
|
||||
console.log(evt.data);
|
||||
};
|
||||
selfwebsocket.onerror = function(evt){
|
||||
console.log(evt);
|
||||
};
|
||||
}
|
||||
|
||||
function tweet(data){
|
||||
selfwebsocket.send(data);
|
||||
}
|
||||
initselfWebSocket();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
BIN
websocket_storage/tester
Executable file
BIN
websocket_storage/tester
Executable file
Binary file not shown.
85
websocket_storage/tmp/index.html
Normal file
85
websocket_storage/tmp/index.html
Normal file
@ -0,0 +1,85 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>WebSocket Test</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<style type="text/css">
|
||||
body {
|
||||
background-color: #789; margin: 0;
|
||||
padding: 0; font: 14px Helvetica, Arial, sans-serif;
|
||||
}
|
||||
div.content {
|
||||
width: 800px; margin: 2em auto; padding: 20px 50px;
|
||||
background-color: #fff; border-radius: 1em;
|
||||
}
|
||||
#messages {
|
||||
border: 2px solid #fec; border-radius: 1em;
|
||||
height: 10em; overflow: scroll; padding: 0.5em 1em;
|
||||
}
|
||||
a:link, a:visited { color: #69c; text-decoration: none; }
|
||||
@media (max-width: 700px) {
|
||||
body { background-color: #fff; }
|
||||
div.content {
|
||||
width: auto; margin: 0 auto; border-radius: 0;
|
||||
padding: 1em;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
var rooms = [];
|
||||
var ws = new WebSocket('ws://' + location.host + '/ws');
|
||||
|
||||
if (!window.console) { window.console = { log: function() {} } };
|
||||
|
||||
ws.onopen = function(ev) { console.log(ev); };
|
||||
ws.onerror = function(ev) { console.log(ev); };
|
||||
ws.onclose = function(ev) { console.log(ev); };
|
||||
ws.onmessage = function(ev) {
|
||||
console.log(ev);
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = ev.data;
|
||||
document.getElementById('messages').appendChild(div);
|
||||
|
||||
};
|
||||
|
||||
window.onload = function() {
|
||||
document.getElementById('send_button').onclick = function(ev) {
|
||||
var msg = document.getElementById('send_input').value;
|
||||
document.getElementById('send_input').value = '';
|
||||
ws.send(msg);
|
||||
};
|
||||
document.getElementById('send_input').onkeypress = function(ev) {
|
||||
if (ev.keyCode == 13 || ev.which == 13) {
|
||||
document.getElementById('send_button').click();
|
||||
}
|
||||
};
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="content">
|
||||
<h1>Websocket PubSub Demonstration</h1>
|
||||
|
||||
<p>
|
||||
This page demonstrates how Mongoose could be used to implement
|
||||
<a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">
|
||||
publish–subscribe pattern</a>. Open this page in several browser
|
||||
windows. Each window initiates persistent
|
||||
<a href="http://en.wikipedia.org/wiki/WebSocket">WebSocket</a>
|
||||
connection with the server, making each browser window a websocket client.
|
||||
Send messages, and see messages sent by other clients.
|
||||
</p>
|
||||
|
||||
<div id="messages">
|
||||
</div>
|
||||
|
||||
<p>
|
||||
<input type="text" id="send_input" />
|
||||
<button id="send_button">Send Message</button>
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue
Block a user