From 58cda9cc7fb79ca9df6746de7f9662bc08dc156a Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 13 Oct 2017 12:29:40 +0200 Subject: initial commit --- src/helpers/password-helpers.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/helpers/password-helpers.js (limited to 'src/helpers/password-helpers.js') diff --git a/src/helpers/password-helpers.js b/src/helpers/password-helpers.js new file mode 100644 index 000000000..7aacaa4d0 --- /dev/null +++ b/src/helpers/password-helpers.js @@ -0,0 +1,36 @@ +import { SHA256 } from 'jshashes'; + +export function hash(password) { + return new SHA256().b64(password); +} + +export function scorePassword(password) { + let score = 0; + if (!password) { + return score; + } + + // award every unique letter until 5 repetitions + const letters = {}; + for (let i = 0; i < password.length; i += 1) { + letters[password[i]] = (letters[password[i]] || 0) + 1; + score += 5.0 / letters[password[i]]; + } + + // bonus points for mixing it up + const variations = { + digits: /\d/.test(password), + lower: /[a-z]/.test(password), + upper: /[A-Z]/.test(password), + nonWords: /\W/.test(password), + }; + + let variationCount = 0; + Object.keys(variations).forEach((key) => { + variationCount += (variations[key] === true) ? 1 : 0; + }); + + score += (variationCount - 1) * 10; + + return parseInt(score, 10); +} -- cgit v1.2.3-70-g09d2