Add scripts, route, favicon (to remove all errors)

parent 2aabf487
favicon.ico

14.7 KB

......@@ -5,6 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hypha Aspect Setup Spike 1</title>
<script src='sodium.js'></script>
<script src='keys.js'></script>
<style>
html {
font-family: sans-serif;
......
......@@ -8,7 +8,33 @@ const options = {
}
https.createServer(options, (request, response) => {
pipeline(fs.createReadStream('index.html'), response)
const url = request.url
console.log(`URL: ${url}`)
switch (url) {
case '/':
pipeline(fs.createReadStream('index.html'), response)
break;
case '/keys.js':
pipeline(fs.createReadStream('keys.js'), response)
break;
case '/sodium.js':
pipeline(fs.createReadStream('sodium.js'), response)
break;
case '/favicon.ico':
pipeline(fs.createReadStream('favicon.ico'), response)
break;
default:
console.log(`Unknown path requested: ${url}`)
response.statusCode = 404
response.end('Not found.')
break;
}
}).listen(443, () => {
console.log('Listening on port 443.')
})
// const sodium = require('libsodium-wrappers')
class Keys {
/**
* keyLength has been set to 32. This to be able to use the secretbox
* method inside libsodium.
*/
constructor (password = null) {
this.privateKey = null
this.publicKey = null
this.derivedKey = null
this.encryptedPrivateSigningKey = null
this.encryptedPrivateSigningKeyNonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES)
this.derivedKeySettings = {
keyLength: sodium.crypto_box_SEEDBYTES,
opslimit: sodium.crypto_pwhash_OPSLIMIT_MODERATE,
// The memory limit here is set low as this runs in the browser (where
// there is a cap on the amount of heap memory that can be used). To
// compensate, we set the opslimit to the moderate constant value
// defined in the library.
//
// For further information, please see:
// https://github.com/jedisct1/libsodium.js#standard-vs-sumo-version
memlimit: 10777215,
algorithm: sodium.crypto_pwhash_ALG_DEFAULT,
saltBytes: sodium.crypto_pwhash_SALTBYTES,
salt: sodium.randombytes_buf(sodium.crypto_pwhash_SALTBYTES)
}
console.log(`Password is ${password}`)
if (password !== null) {
// If a password is provided, create initialise the object with it.
this.generateSigningKeys()
this.createDerivedKeyFromPassword(password)
this.encryptPrivateSigningKeyWithDerivedKey()
}
}
// Public methods
get keysObject () {
const _keysObject = {
salt: this.stringify(this.derivedKeySettings.salt),
nonce: this.stringify(this.encryptedPrivateSigningKeyNonce),
publicKey: this.stringify(this.publicKey),
privateKey: this.stringify(this.encryptedPrivateSigningKey)
}
return _keysObject
}
// Private methods
generateSigningKeys () {
const keyPair = sodium.crypto_sign_keypair()
this.publicKey = keyPair.publicKey
this.privateKey = keyPair.privateKey
}
createDerivedKeyFromPassword (password) {
this.derivedKey = sodium.crypto_pwhash(this.derivedKeySettings.keyLength, password, this.derivedKeySettings.salt, this.derivedKeySettings.opslimit, this.derivedKeySettings.memlimit, this.derivedKeySettings.algorithm)
}
encryptPrivateSigningKeyWithDerivedKey () {
this.encryptedPrivateSigningKey = sodium.crypto_secretbox_easy(this.privateKey, this.encryptedPrivateSigningKeyNonce, this.derivedKey)
}
decryptPrivateSigningKeyWithDerivedKey () {
this.privateKey = sodium.crypto_secretbox_open_easy(this.encryptedPrivateSigningKey, this.encryptedPrivateSigningKeyNonce, this.derivedKey)
}
signMessage (nonce) {
return sodium.crypto_sign(nonce, this.privateKey)
}
stringify (value) {
return sodium.to_hex(value)
}
parse (value) {
return sodium.from_hex(value)
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment