Skip to content

Commit

Permalink
Add zxcvbn for password strength estimation (#555)
Browse files Browse the repository at this point in the history
* add zxcvbn

* use zxcvbn for password estimations

* add comments

* update snapshot
  • Loading branch information
Bruno Barbieri authored Mar 26, 2019
1 parent e7e3fca commit edee2a9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ exports[`ChoosePassword should render correctly 1`] = `
style={
Object {
"alignSelf": "flex-end",
"backgroundColor": "#FFFFFF",
"marginTop": 8,
"position": "absolute",
}
Expand Down
28 changes: 12 additions & 16 deletions app/components/Views/ChoosePassword/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { getOnboardingNavbarOptions } from '../../UI/Navbar';
import SecureKeychain from '../../../core/SecureKeychain';
import Icon from 'react-native-vector-icons/FontAwesome';
import AppConstants from '../../../core/AppConstants';
import zxcvbn from 'zxcvbn';

const styles = StyleSheet.create({
mainWrapper: {
Expand Down Expand Up @@ -127,6 +128,7 @@ const styles = StyleSheet.create({
color: colors.brightGreen
},
showHideToggle: {
backgroundColor: colors.white,
position: 'absolute',
marginTop: 8,
alignSelf: 'flex-end'
Expand Down Expand Up @@ -283,12 +285,19 @@ class ChoosePassword extends Component {
};

getPasswordStrengthWord() {
// this.state.passwordStrength is calculated by zxcvbn
// which returns a score based on "entropy to crack time"
// 0 is the weakest, 4 the strongest
switch (this.state.passwordStrength) {
case 0:
return 'weak';
case 1:
return 'weak';
case 2:
return 'good';
return 'weak';
case 3:
return 'good';
case 4:
return 'strong';
}
}
Expand Down Expand Up @@ -330,22 +339,9 @@ class ChoosePassword extends Component {
};

onPasswordChange = val => {
let strength = 1;

// If the password length is greater than 6 and contain alphabet,number,special character respectively
if (
val.length > 6 &&
((val.match(/[a-z]/) && val.match(/\d+/)) ||
(val.match(/\d+/) && val.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) ||
(val.match(/[a-z]/) && val.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)))
)
strength = 2;

// If the password length is greater than 6 and must contain alphabets,numbers and special characters
if (val.length > 6 && val.match(/[a-z]/) && val.match(/\d+/) && val.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/))
strength = 3;
const passInfo = zxcvbn(val);

this.setState({ password: val, passwordStrength: strength });
this.setState({ password: val, passwordStrength: passInfo.score });
};

toggleShowHide = () => {
Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
"url": "0.11.0",
"url-parse": "1.4.4",
"valid-url": "1.0.9",
"vm-browserify": "0.0.4"
"vm-browserify": "0.0.4",
"zxcvbn": "4.4.2"
},
"devDependencies": {
"@babel/core": "7.3.4",
Expand Down

0 comments on commit edee2a9

Please sign in to comment.