-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae06d3c
commit da7771b
Showing
7 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.compare = compare; | ||
const colors_1 = require("../utils/colors"); | ||
const hash_1 = require("./hash"); | ||
const prompt_1 = require("./prompt"); | ||
function compare() { | ||
const rl = (0, prompt_1.createprompt)(); | ||
(0, prompt_1.dialog)(rl, '\nEnter the input to compare: ', (input) => { | ||
(0, prompt_1.dialog)(rl, '\nEnter the hash to compare: ', (hash) => { | ||
const separator = hash.split(':'); | ||
if (!separator || separator.length !== 2) { | ||
console.error(`${colors_1.colors.red}Invalid hash format!${colors_1.colors.reset} The format of a full hash is: ${colors_1.colors.yellow}<salt>${colors_1.colors.reset}:${colors_1.colors.green}<hash>${colors_1.colors.reset}`); | ||
rl.close(); | ||
compare(); | ||
return; | ||
} | ||
console.log(`\n${colors_1.colors.yellow}Now I need the ${colors_1.colors.cyan}iterations${colors_1.colors.yellow} and ${colors_1.colors.cyan}keysize${colors_1.colors.yellow} to compare the hash with the input.${colors_1.colors.reset}\n ${colors_1.colors.red}[WARN]${colors_1.colors.reset} ${colors_1.colors.gray}The iterations and the keysize should be the same as the ones used to generate the hash.${colors_1.colors.reset}`); | ||
(0, prompt_1.dialog)(rl, '\nLet\'s start with the number of the iterations: ', (iter) => { | ||
const iterations = parseInt(iter, 10); | ||
if (isNaN(iterations)) { | ||
console.error(`${colors_1.colors.red}The iterations should be a number.${colors_1.colors.reset}`); | ||
rl.close(); | ||
compare(); | ||
return; | ||
} | ||
(0, prompt_1.dialog)(rl, '\nNow with the key size (number of bytes): ', (key) => { | ||
const keysize = parseInt(key, 10); | ||
if (isNaN(keysize)) { | ||
console.error(`${colors_1.colors.red}The key size should be a number.${colors_1.colors.reset}`); | ||
rl.close(); | ||
compare(); | ||
return; | ||
} | ||
if ((0, hash_1.verify)(input, hash, iterations, keysize)) { | ||
console.log(`${colors_1.colors.green}The input matches the hash.${colors_1.colors.reset}`); | ||
} | ||
else { | ||
console.error(`${colors_1.colors.red}The input doesn't match the hash.${colors_1.colors.reset}`); | ||
} | ||
rl.close(); | ||
compare(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || (function () { | ||
var ownKeys = function(o) { | ||
ownKeys = Object.getOwnPropertyNames || function (o) { | ||
var ar = []; | ||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; | ||
return ar; | ||
}; | ||
return ownKeys(o); | ||
}; | ||
return function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hash = hash; | ||
exports.verify = verify; | ||
const crypto = __importStar(require("crypto")); | ||
const colors_1 = require("../utils/colors"); | ||
function hash(input, saltsize, keysize, iterations) { | ||
const salt = crypto.randomBytes(saltsize).toString('hex'); | ||
const hashed = crypto.pbkdf2Sync(input, salt, iterations, keysize, 'sha256').toString('hex'); | ||
console.log('====================================='); | ||
console.log(`${colors_1.colors.yellow}${salt}${colors_1.colors.reset}:${colors_1.colors.green}${hashed}${colors_1.colors.reset}`); | ||
console.log(`${colors_1.colors.yellow}salt - yellow${colors_1.colors.reset}, ${colors_1.colors.green}hash - green${colors_1.colors.reset}`); | ||
console.log(`salt length: ${salt.length} characters / hash length: ${hashed.length} characters / total length: ${salt.length + hashed.length + 1} characters`); // that 1 is for ":" separator :) | ||
console.log(`input: ${input} | salt: ${saltsize} bytes, key: ${keysize} bytes, iterations: ${iterations}`); | ||
console.log('====================================='); | ||
return `${salt}:${hashed}`; // the actual format | ||
} | ||
function verify(input, hash, iterations, keysize) { | ||
try { | ||
const separator = hash.split(':'); | ||
const [salt, halfhash] = separator; | ||
const iter = iterations; | ||
const key = keysize; | ||
const derive = crypto.pbkdf2Sync(input, salt, iter, key, 'sha256'); | ||
return derive.toString('hex') === halfhash; | ||
} | ||
catch (e) { | ||
console.error(`${colors_1.colors.red}Something went wrong:\n${e}${colors_1.colors.reset}`); | ||
return false; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || (function () { | ||
var ownKeys = function(o) { | ||
ownKeys = Object.getOwnPropertyNames || function (o) { | ||
var ar = []; | ||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; | ||
return ar; | ||
}; | ||
return ownKeys(o); | ||
}; | ||
return function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createprompt = createprompt; | ||
exports.dialog = dialog; | ||
exports.prompt = prompt; | ||
const readline = __importStar(require("readline")); | ||
const cmdhandler_1 = require("../utils/cmdhandler"); | ||
function createprompt() { | ||
return readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
} | ||
function dialog(rl, query, callback) { | ||
rl.question(query, (answer) => { | ||
if (!(0, cmdhandler_1.cmds)(answer.trim().toLowerCase(), rl)) { | ||
callback(answer); | ||
} | ||
}); | ||
} | ||
function prompt(saltsize, keysize, iterations) { | ||
const rl = createprompt(); | ||
rl.question('Enter something to be hashed: ', (input) => { | ||
const cmd = input.trim().toLowerCase(); | ||
if (!(0, cmdhandler_1.cmds)(cmd, rl)) { | ||
const { hash } = require('./hash'); | ||
hash(input, saltsize, keysize, iterations); | ||
rl.close(); | ||
prompt(saltsize, keysize, iterations); | ||
} | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.start = start; | ||
const prompt_1 = require("./prompt"); | ||
function start() { | ||
const rl = (0, prompt_1.createprompt)(); | ||
(0, prompt_1.dialog)(rl, 'Enter a salt number (number of bytes): ', (salt) => { | ||
(0, prompt_1.dialog)(rl, 'Enter key number (number of bytes): ', (key) => { | ||
(0, prompt_1.dialog)(rl, 'Enter number of iterations: ', (iter) => { | ||
const saltsize = parseInt(salt, 10); | ||
const keysize = parseInt(key, 10); | ||
const iterations = parseInt(iter, 10); | ||
if (isNaN(saltsize) || isNaN(keysize) || isNaN(iterations)) { | ||
console.error('\x1b[31m%s\x1b[0m', 'All inputs must be valid numbers.'); | ||
rl.close(); | ||
start(); | ||
return; | ||
} | ||
console.log(`Ready to hash with ${saltsize} bytes saltsize, ${keysize} bytes keysize and ${iterations} iterations.`); | ||
rl.close(); | ||
(0, prompt_1.prompt)(saltsize, keysize, iterations); | ||
}); | ||
}); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const start_1 = require("./funcs/start"); | ||
console.log('\x1b[32m%s\x1b[0m', 'Example: 8 bytes salt, 32 bytes key, 10000 iterations'); | ||
(0, start_1.start)(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || (function () { | ||
var ownKeys = function(o) { | ||
ownKeys = Object.getOwnPropertyNames || function (o) { | ||
var ar = []; | ||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; | ||
return ar; | ||
}; | ||
return ownKeys(o); | ||
}; | ||
return function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cmds = cmds; | ||
const fs = __importStar(require("fs")); | ||
const path = __importStar(require("path")); | ||
const start_1 = require("../funcs/start"); | ||
const colors_1 = require("./colors"); | ||
const comparator_1 = require("../funcs/comparator"); | ||
function cmds(cmd, rl) { | ||
if (cmd === 'exit') { | ||
rl.close(); | ||
return true; | ||
} | ||
if (cmd === 'restart') { | ||
rl.close(); | ||
console.clear(); | ||
console.log(`${colors_1.colors.green}The application had been restarted! :*${colors_1.colors.reset}`); | ||
(0, start_1.start)(); | ||
return true; | ||
} | ||
if (cmd === 'compare') { | ||
rl.close(); | ||
(0, comparator_1.compare)(); | ||
return true; | ||
} | ||
if (cmd === 'start' || cmd === 'hub' || cmd === 'back') { | ||
rl.close(); | ||
console.log(`${colors_1.colors.green}You had been teleported back to the start of the application! ${colors_1.colors.magenta}:-)${colors_1.colors.reset}`); | ||
(0, start_1.start)(); | ||
return true; | ||
} | ||
if (cmd === 'help') { | ||
console.log(`\n${colors_1.colors.cyan}Here are the available commands:${colors_1.colors.reset}`); | ||
console.log(`${colors_1.colors.yellow}exit${colors_1.colors.reset} - to exit the application.`); | ||
console.log(`${colors_1.colors.yellow}restart${colors_1.colors.reset} - to restart the application. (and clears the console)`); | ||
console.log(`${colors_1.colors.yellow}compare${colors_1.colors.reset} - to compare an input with a hash.`); | ||
console.log(`${colors_1.colors.yellow}start/hub/back${colors_1.colors.reset} - will turn you back to hashing your input.`); | ||
console.log(`${colors_1.colors.yellow}delcompiled${colors_1.colors.reset} - will delete the 'compiled' folder.`); | ||
console.log(`${colors_1.colors.yellow}help${colors_1.colors.reset} - to show this message.\n`); | ||
rl.close(); | ||
(0, start_1.start)(); | ||
return true; | ||
} | ||
if (cmd === 'delcompiled') { // useful if you compile and recompile a lot :D | ||
const dir = path.join(__dirname, '../../compiled'); | ||
if (fs.existsSync(dir)) { | ||
fs.rmSync(dir, { recursive: true, force: true }); | ||
console.log(`${colors_1.colors.green}The 'compiled' folder has been deleted successfully.${colors_1.colors.reset}`); | ||
} | ||
else { | ||
console.log(`${colors_1.colors.red}The 'compiled' folder doesnt exist.${colors_1.colors.reset}`); | ||
} | ||
rl.close(); | ||
(0, start_1.start)(); | ||
return true; | ||
} | ||
return false; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.colors = void 0; | ||
exports.colors = { | ||
black: '\x1b[30m', | ||
red: '\x1b[31m', | ||
green: '\x1b[32m', | ||
yellow: '\x1b[33m', | ||
blue: '\x1b[34m', | ||
magenta: '\x1b[35m', | ||
cyan: '\x1b[36m', | ||
white: '\x1b[37m', | ||
gray: '\x1b[90m', | ||
reset: '\x1b[0m' | ||
}; |