on cd4bc68 help sign in
unity8/qml/Wizard/PasswordMeter.qml
99 lines 2.9 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17import QtQuick 2.4
18import Ubuntu.Components 1.2
20Item {
21 property string password: ""
22 readonly property int passwordScore: scorePassword(password)
24 function scorePassword(pass) {
25 var score = 0;
26 if (!pass)
27 return score;
29 // award every unique letter until 5 repetitions
30 var letters = Object();
31 for (var i=0; i<pass.length; i++) {
32 letters[pass[i]] = (letters[pass[i]] || 0) + 1;
33 score += 5.0 / letters[pass[i]];
34 }
36 // bonus points for mixing it up
37 var variations = {
38 digits: /\d/.test(pass),
39 lower: /[a-z]/.test(pass),
40 upper: /[A-Z]/.test(pass),
41 nonWords: /\W/.test(pass),
42 }
44 var variationCount = 0;
45 for (var check in variations) {
46 variationCount += (variations[check] === true) ? 1 : 0;
47 }
48 score += (variationCount - 1) * 10;
50 return parseInt(score);
51 }
53 Rectangle {
54 id: passwordStrengthMeter
55 anchors {
56 left: parent.left
57 right: parent.right
58 }
59 width: parent.width
60 height: units.gu(1)
61 color: {
62 if (passwordScore > 80)
63 return okColor;
64 else if (passwordScore > 60)
65 return "#f9c00f";
66 else if (passwordScore >= 30)
67 return "#ed3146";
69 return "#ed3146";
70 }
71 visible: password.length > 0
72 }
74 Label {
75 id: passwordStrengthInfo
76 anchors {
77 left: parent.left
78 right: parent.right
79 top: passwordStrengthMeter.bottom
80 }
81 wrapMode: Text.Wrap
82 text: {
83 if (password.length < 8)
84 return i18n.tr("Password too short")
85 else if (passwordScore > 80)
86 return i18n.tr("Strong password");
87 else if (passwordScore > 60)
88 return i18n.tr("Fair password")
89 if (passwordScore >= 30)
90 return i18n.tr("Weak password");
92 return i18n.tr("Very weak password");
93 }
94 color: "#888888"
95 fontSize: "small"
96 font.weight: Font.Light
97 visible: password.length > 0
98 }