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
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | |
/* SHA-3 (FIPS 202) ‘Keccak’ reference implementation in JavaScript (c) Chris Veness 2016-2017 */ | |
/* MIT Licence */ | |
/* www.movable-type.co.uk/scripts/sha3.html */ | |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | |
'use strict'; | |
/** |
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
/** | |
* Encrypts plaintext using AES-GCM with supplied password, for decryption with aesGcmDecrypt(). | |
* (c) Chris Veness MIT Licence | |
* | |
* @param {String} plaintext - Plaintext to be encrypted. | |
* @param {String} password - Password to use to encrypt plaintext. | |
* @returns {String} Encrypted ciphertext. | |
* | |
* @example | |
* const ciphertext = await aesGcmEncrypt('my secret text', 'pw'); |
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
/** | |
* Returns PBKDF2 derived key from supplied password. | |
* | |
* Stored key can subsequently be used to verify that a password matches the original password used | |
* to derive the key, using pbkdf2Verify(). | |
* | |
* @param {String} password - Password to be hashed using key derivation function. | |
* @param {Number} [iterations=1e6] - Number of iterations of HMAC function to apply. | |
* @returns {String} Derived key as base64 string. | |
* |
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
/** | |
* Returns SHA-256 hash from supplied message. | |
* | |
* @param {String} message. | |
* @returns {String} hash as hex string. | |
* | |
* @example | |
* sha256('abc').then(hash => console.log(hash)); | |
* const hash = await sha256('abc'); | |
*/ |
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
/** | |
* Returns standard deviation of set of values. | |
* | |
* @param {number[]} values - Array of values. | |
* @returns {number} Standard devation of values. | |
*/ | |
function stdDeviation(values) { | |
const avgOfValues = average(values); | |
const squaresOfDiffs = values.map(value => (value-avgOfValues)**2); | |
const avgOfSquaresOfDiffs = average(squaresOfDiffs); |
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
/** | |
* Generates a MongoDB-style ObjectId in Node.js. Uses nanosecond timestamp in place of counter; | |
* should be impossible for same process to generate multiple objectId in same nanosecond? (clock | |
* drift can result in an *extremely* remote possibility of id conflicts). | |
* | |
* @returns {string} Id in same format as MongoDB ObjectId. | |
*/ | |
function objectId() { | |
const os = require('os'); | |
const crypto = require('crypto'); |
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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>AES client/server test</title> | |
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.min.css"> | |
<style> | |
body { font-size: 80%; padding: 1em; } | |
form { margin-top: 2em; } | |
label { display: inline-block; width: 6em; } |
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
var koa = require('koa'); | |
var session = require('koa-session'); | |
var app = module.exports = koa(); | |
app.keys = ['some secret hurr']; | |
app.use(session({ domain: '.app.localhost' }, app)); // THIS WORKS IN BROWSER BUT FAILS IN SUPERTEST | |
//app.use(session(app)); // THIS WORKS EITHER WAY | |
app.use(function*() { |
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
var koa = require('koa'); | |
var session = require('koa-session'); | |
var flash = require('koa-flash'); | |
var router = require('koa-router'); | |
var handlebars = require("koa-handlebars"); | |
var app = koa(); | |
app.keys = ['foo']; | |
app.use(session({ domain: '.localhost' }, app)); // THIS FAILS |
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
/** | |
* Returns camel-cased equivalent of (lower-case) hyphenated string. | |
* | |
* To enable round-tripping, hyphens not followed by a-z are left intact | |
* (can be checked for and/or removed manually if required). | |
* | |
* Only transforms ASCII capitals (lack of JavaScript Unicode regexp). | |
*/ | |
function hyphenToCamel(str) { | |
// for Unicode transforms, replace [a-z] with \p{Ll} if available |
NewerOlder