Skip to content

Commit

Permalink
Add Argon2 hash operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenonym committed Oct 7, 2019
1 parent 0670894 commit 9c9c8b0
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"babel-plugin-dynamic-import-node": "^2.3.0",
"base64-loader": "^1.0.0",
"chromedriver": "^76.0.1",
"colors": "^1.3.3",
"copy-webpack-plugin": "^5.0.4",
Expand Down Expand Up @@ -85,6 +86,7 @@
"dependencies": {
"@babel/polyfill": "^7.4.4",
"@babel/runtime": "^7.5.5",
"argon2-browser": "^1.11.1",
"arrive": "^2.4.1",
"babel-plugin-transform-builtin-extend": "1.1.2",
"bcryptjs": "^2.4.3",
Expand Down
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
"Bcrypt compare",
"Bcrypt parse",
"Scrypt",
"Argon2",
"Fletcher-8 Checksum",
"Fletcher-16 Checksum",
"Fletcher-32 Checksum",
Expand Down
99 changes: 99 additions & 0 deletions src/core/operations/Argon2.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* @author Tan Zhen Yong [tzy@beyondthesprawl.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import argon2 from "argon2-browser";

/**
* Argon2 operation
*/
class Argon2 extends Operation {

/**
* Argon2 constructor
*/
constructor() {
super();

this.name = "Argon2";
this.module = "Crypto";
this.description = "Argon2 is a key derivation function that was selected as the winner of the Password Hashing Competition in July 2015. It was designed by Alex Biryukov, Daniel Dinu, and Dmitry Khovratovich from the University of Luxembourg.<br><br>Enter the password in the input to generate its hash.";
this.infoURL = "https://wikipedia.org/wiki/Argon2";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "Salt",
"type": "string",
"value": "somesalt"
},
{
"name": "Iterations",
"type": "number",
"value": 3
},
{
"name": "Memory (KiB)",
"type": "number",
"value": 4096
},
{
"name": "Parallelism",
"type": "number",
"value": 1
},
{
"name": "Hash length (bytes)",
"type": "number",
"value": 32
},
{
"name": "Type",
"type": "option",
"value": ["Argon2i", "Argon2d", "Argon2id"],
"defaultIndex": 0
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const argon2Types = {
"Argon2i": argon2.ArgonType.Argon2i,
"Argon2d": argon2.ArgonType.Argon2d,
"Argon2id": argon2.ArgonType.Argon2id
};

const salt = args[0],
time = args[1],
mem = args[2],
parallelism = args[3],
hashLen = args[4],
type = argon2Types[args[5]];

return argon2.hash({
pass: input,
salt,
time,
mem,
parallelism,
hashLen,
type,
}).then(res => {
return res.encoded;
}).catch(err => {
throw new OperationError(`Error: ${err.message}`);
});
}

}

export default Argon2;
5 changes: 5 additions & 0 deletions tests/node/tests/operations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ Tiger-128`;
assert.strictEqual(result.toString(), "Szkkb zh z Xozn");
}),

it("Argon2", async () => {
const result = await chef.Argon2("argon2password");
assert.strictEqual(result.toString(), "$argon2i$v=19$m=4096,t=3,p=1$c29tZXNhbHQ$s43my9eBljQADuF/LWCG8vGqwAJzOorKQ0Yog8jFvbw");
}),

it("Bcrypt", async () => {
const result = await chef.bcrypt("Put a Sock In It");
assert.strictEqual(result.toString(), "$2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6");
Expand Down
9 changes: 9 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ module.exports = {
},
},
module: {
// argon2-browser loads argon2.wasm by itself, so Webpack should not load it
noParse: /node_modules\/argon2-browser\/dist\/argon2\.wasm$/,
rules: [
{
test: /\.m?js$/,
Expand All @@ -77,6 +79,13 @@ module.exports = {
type: "javascript/auto",
loader: "babel-loader"
},
{
test: /node_modules\/argon2-browser\/dist\/argon2\.wasm$/,
// Load argon2.wasm as base64-encoded binary file
// expected by argon2-browser
loaders: "base64-loader",
type: "javascript/auto"
},
{
test: /forge.min.js$/,
loader: "imports-loader?jQuery=>null"
Expand Down

0 comments on commit 9c9c8b0

Please sign in to comment.