-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSystem.Security.Password.debug.js
More file actions
2102 lines (2006 loc) · 74.9 KB
/
System.Security.Password.debug.js
File metadata and controls
2102 lines (2006 loc) · 74.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=============================================================================
// Jocys.com JavaScript.NET Classes (In C# Object Oriented Style)
// Created by Evaldas Jocys <evaldas@jocys.com>
//=============================================================================
/// <reference path="System.debug.js" />
//=============================================================================
// Namespaces
//-----------------------------------------------------------------------------
// <PropertyGroup>
// <RootNamespace>System.Security.Password</RootNamespace>
// <PropertyGroup>
//-----------------------------------------------------------------------------
System.Type.RegisterNamespace("System.Security.Password");
//=============================================================================
//------------------------------------------------------------------------------
// Created by: Evaldas Jocys (https://www.jocys.com/evaldas/)
// Updated by: Your Name...
//------------------------------------------------------------------------------
// UPDATES:
//------------------------------------------------------------------------------
// 2005-01-31 First working version;
//------------------------------------------------------------------------------
// NOTE: If you planing to improve this script, send note to us about it, please.
//==============================================================================
// Classes:
// Password.Generator - password generator.
// Password.Presets = collection of presets.
// Password.Preset = password preset.
// Password.Calls = collection of calls.
// Password.Call = password call class.
// Password.Word = password item class.
// Static classes:
// System.Security.Password.Templates.Charsets
// Static templates.
// System.Security.Password.Templates.Presets - used by
// System.Security.Password.Templates.Calls
// System.Security.Password.Templates.Filters
//=============================================================================
// CLASS: Interface
//-----------------------------------------------------------------------------
// Interface namespace required System.js file to be included.
System.Security.Password.Interface = System.Security.Password.Interface ? System.Security.Password.Interface : {};
System.Security.Password.Interface.PassGen = null;
System.Security.Password.Interface.PassGenWindow = null;
System.Security.Password.Interface.Controls = {};
System.Security.Password.Interface.Controls.ByGenerate = {};
System.Security.Password.Interface.Controls.ByAdvanced = {};
System.Security.Password.Interface.AddControl = function (cp, control, name) {
if (control) {
cp[name] = typeof control === "string" ? document.getElementById(control) : control;
}
};
System.Security.Password.Interface.AutoIfEmpty = false;
System.Security.Password.Interface.AddControls = function (passwordTextBox, confirmTextBox, generateButton, advancedButton, showPassCheckBox, autoIfEmpty) {
var cp = {};
System.Security.Password.Interface.AddControl(cp, passwordTextBox, "Password");
System.Security.Password.Interface.AddControl(cp, confirmTextBox, "Confirm");
System.Security.Password.Interface.AddControl(cp, generateButton, "Generate");
System.Security.Password.Interface.AddControl(cp, advancedButton, "Advanced");
System.Security.Password.Interface.AddControl(cp, showPassCheckBox, "ShowPass");
System.Security.Password.Interface.AutoIfEmpty = autoIfEmpty === true;
if (cp.Generate) {
System.Security.Password.Interface.Controls.ByGenerate[cp.Generate.id] = cp;
}
if (cp.Advanced) {
System.Security.Password.Interface.Controls.ByAdvanced[cp.Advanced.id] = cp;
}
return cp;
};
System.Security.Password.Interface.CheckConfirmAndShow = function (cp) {
if (cp.Confirm) {
cp.Confirm.value = "";
// Focus inside confirm field so user can confirm password if he wants.
cp.Confirm.value = cp.Password.value;
cp.Confirm.focus();
cp.Confirm.select();
}
if (cp.ShowPass) {
// Reveal password to the user.
if (cp.ShowPass.checked === false) cp.ShowPass.click();
}
};
System.Security.Password.Interface.TextBoxFocusHandler = function (sender, e) {
/// <summary>
/// Generate password and fill TextBox with it if user focus on empty password textbox.
/// </summary>
if (sender.value.length === 0 && System.Security.Password.Interface.AutoIfEmpty) {
sender.value = System.Security.Password.Interface.PassGen.NewPassword().Text;
sender.select();
System.Security.Password.Interface.CheckConfirmAndShow(e.Controls);
}
};
System.Security.Password.Interface.GenerateClickHandler = function (sender, e) {
/// <summary>
/// Generate password and fill TextBox with it.
/// </summary>
e.Controls.Password.value = System.Security.Password.Interface.PassGen.NewPassword().Text;
e.Controls.Password.select();
System.Security.Password.Interface.CheckConfirmAndShow(e.Controls);
};
System.Security.Password.Interface.AdvancedClickHandler = function (sender, e) {
/// <summary>
/// Open advanced window with password generator.
/// </summary>
var id = e.Controls.Password.id;
var path = System.GetScriptsPath();
var url = path + "/Examples/System.Security.Password.Frameset.htm?Field=" + id;
// Please note thatn MSIE 7 forces the presence of the Address Bar by default
// A missing address bar creates a chance for a fraudster to forge an address of their own.
// To help thwart that, IE7 will show the address bar on all internet windows to help users see where they are.
// coming from Microsoft Internet Explorer Blog, Better Website Identification
// Mozilla.org also intends to soon force the presence of the Location Bar in Firefox 3.
System.Security.Password.Interface.PassGenWindow = window.open(url, "PassGenWindow", "channelmode=no,directories=no,fullscreen=no,width=850,height=600,location=no,menubar=no,resizable=yes,scrollbars=no,status=yes,titlebar=no,toolbar=no");
System.Security.Password.Interface.PassGenWindow.focus();
};
System.Security.Password.Interface.CheckPassGen = function () {
if (!System.Security.Password.Interface.PassGen) {
System.Security.Password.Interface.PassGen = new System.Security.Password.Generator();
}
};
System.Security.Password.Interface.Attach = function (passwordTextBox, confirmTextBox, generateButton, advancedButton, showPassCheckBox) {
System.Security.Password.Interface.CheckPassGen();
var cp = System.Security.Password.Interface.AddControls(passwordTextBox, confirmTextBox, generateButton, advancedButton, showPassCheckBox);
var e = new System.EventArgs("Focus");
e["Controls"] = cp;
if (cp.Password) {
var subHandler = function () { System.Security.Password.Interface.TextBoxFocusHandler(cp.Password, e); };
Events.Add(cp.Password, "focus", new System.EventHandler(this, subHandler), true);
}
if (cp.Generate) {
var subHandler2 = function () { System.Security.Password.Interface.GenerateClickHandler(cp.Password, e); };
Events.Add(cp.Generate, "click", new System.EventHandler(this, subHandler2), true);
}
if (cp.Advanced) {
var subHandler3 = function () { System.Security.Password.Interface.AdvancedClickHandler(cp.Password, e); };
Events.Add(cp.Advanced, "click", new System.EventHandler(this, subHandler3), true);
}
};
//=============================================================================
// CLASS: Calls
//-----------------------------------------------------------------------------
System.Security.Password.CssPrefix = "SWUI_PG";
System.Security.Password.CharTypeEnum = {
Numbers: 1,
Uppercase: 2,
Lowercase: 4,
Extended: 8,
Extra: 16
};
System.Security.Password.FilterTypeEnum = {
Remember: 1,
Keyboard: 2,
Phone: 4,
Ascii: 8,
Chars: 16
};
//=============================================================================
// CLASS: Char
//-----------------------------------------------------------------------------
System.Security.Password.Char = function (code) {
//---------------------------------------------------------
// Public properties.
this.Char = "";
this.Code = 0;
this.Case = "";
this.Name = "";
this.Html = "";
this.Category = "";
//---------------------------------------------------------
// Private properties.
var me = this;
//---------------------------------------------------------
this.ToHtml = function () {
var cssPrefix = System.Security.Password.CssPrefix;
var call = System.Security.Password.Templates.Calls.EN_Unicode;
var html = "";
html += "<span class=\"" + cssPrefix + "_Char_" + this.Category + "\">" + this.Char + "</span>";
html += "<span class=\"" + cssPrefix + "_Char_" + this.Category + "_Name\">" + this.Name + "</span>";
return html;
};
//---------------------------------------------------------
this.InitializeClass = function () {
// Get code from string if required.
code = typeof code === "string" ? code.charCodeAt(0) : code;
this.Code = typeof code === "number" ? code : 0;
this.Char = code ? String.fromCharCode(code) : "";
this.Case = "Unknown";
this.Name = name ? name : "";
// Determine category of char.
var category = "Unknown";
if (code >= 32 && code <= 47) { category = "Symbol"; }
else if (code >= 48 && code <= 57) { category = "Number"; }
else if (code >= 58 && code <= 64) { category = "Symbol"; }
else if (code >= 65 && code <= 90) { category = "Letter"; }
else if (code >= 91 && code <= 96) { category = "Symbol"; }
else if (code >= 97 && code <= 122) { category = "Letter"; }
else if (code >= 123 && code <= 126) { category = "Symbol"; }
else if (code >= 128 && code <= 255) { category = "Extra"; }
else if (code === 32) { category = "Special"; }
this.Category = category;
// Determine Case.
var lowerCode = this.Char.toLowerCase().charCodeAt(0);
var upperCode = this.Char.toUpperCase().charCodeAt(0);
if (lowerCode === upperCode) this.Case = "";
else if (this.Code === lowerCode) this.Case = "Lower";
else if (this.Code === upperCode) this.Case = "Upper";
// Set name. Use basic template.
var call = System.Security.Password.Templates.Calls.EN_Unicode;
this.Name = call[this.Code] ? call[this.Code][1] : "";
// Create HTML.
this.Html = this.ToHtml();
};
this.InitializeClass();
};
//=============================================================================
// CLASS: Calls
//-----------------------------------------------------------------------------
System.Security.Password.Calls = function (callName) {
/// <summary>
/// Represents a collection of password Calls.
/// </summary>
//---------------------------------------------------------
// Public properties.
this.Items;
//---------------------------------------------------------
// Private properties.
var me = this;
//---------------------------------------------------------
// Load all call items from templates;
this.LoadAll = function () {
for (var property in System.Security.Password.Templates.Calls) {
this.Load(property);
}
};
//---------------------------------------------------------
// Convert bytes to array of calls.
this.ToArray = function (bytes) {
var calls = [];
for (var i = 0; i < bytes.length; i++) {
//var call = new
}
};
//---------------------------------------------------------
// Load all items from templates;
this.Load = function (name) {
// If name was specified then...
if (name) {
// Load single call.
this.Items[property] = new System.Security.Password.Call(property);
this.Items[property]["Parent"] = this;
} else {
// Load all calls.
for (var property in System.Security.Password.Templates.Calls) {
this.Items[property] = new System.Security.Password.Call(property);
this.Items[property]["Parent"] = this;
}
}
};
//---------------------------------------------------------
this.InitializeClass = function () {
this.Items = {};
this.Load(callName);
};
this.InitializeClass();
};
//=============================================================================
// CLASS: Security.Password.Call
//-----------------------------------------------------------------------------
System.Security.Password.Call = function (name) {
/// <summary>
/// Password Call is collection of words related to characters.
/// </summary>
//---------------------------------------------------------
// Public properties.
this.Chars;
this.Data;
this.Name;
//---------------------------------------------------------
// Private properties.
var me = this;
//---------------------------------------------------------
this.CharToHtml = function (c, type) {
/// <summary>
/// Convert char to HTML code.
/// </summary>
var cssPrefix = System.Security.Password.CssPrefix;
var html = "";
switch (type) {
case "Table":
html += "<tr>";
html += "<td class=\"{prefix}_Index{odd}\">{index}</td>";
html += "<td class=\"{prefix}_Code{odd}\">{code}</td>";
html += "<td class=\"{prefix}_Char{odd} {category}_Char\">{char}</td>";
//html += "<td class=\"{prefix}_Name {category}_Name\">{name}</td>";
html += "<td class=\"{prefix}_Call{odd} {category}_Call{class}\">{call}</td>";
html += "</tr>";
break;
default:
html += "<div>";
html += "<span class=\"{prefix}_Index{odd}\">{index}</span>";
html += "<span class=\"{prefix}_Code{odd}\">{code}</span>";
html += "<span class=\"{prefix}_Char{odd} {category}_Char\">{char}</span>";
//html += "<span class=\"{prefix}_Name {category}_Name\">{name}</span>";
html += "<span class=\"{prefix}_Call{odd} {category}_Call{class}\">{call}</span>";
html += "</div>";
break;
}
var ch = new System.Security.Password.Char(c);
// Category can be: Symbol, Number, Letter, Extra, Special.
var category = cssPrefix + "_" + ch.Category;
html = html.replace(new RegExp("{prefix}", "g"), cssPrefix);
html = html.replace(new RegExp("{category}", "g"), category);
html = html.replace(new RegExp("{char}", "g"), ch.Char);
html = html.replace(new RegExp("{name}", "g"), ch.Name);
var code = new System.Int32(ch.Code).ToString("X4");
html = html.replace(new RegExp("{code}", "g"), code);
var callName = this.Data[ch.Code] ? this.Data[ch.Code][1] : "";
html = html.replace(new RegExp("{call}", "g"), callName);
var callClass = this.Name ? " " + cssPrefix + "_" + this.Name : "";
html = html.replace(new RegExp("{class}", "g"), callClass);
return html;
};
//---------------------------------------------------------
this.ToHtml = function (c, type) {
var cssPrefix = System.Security.Password.CssPrefix;
var rxOdd = new RegExp("{odd}", "g");
var rxIdx = new RegExp("{index}", "g");
var html = "";
if (type === "Table") html += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"" + cssPrefix + "_Table\">";
for (var i = 0; i < c.length; i++) {
html += this.CharToHtml(c.charAt(i), type);
var odd = i % 2;
odd = "_" + new String(odd);
html = html.replace(rxOdd, odd, "g");
html = html.replace(rxIdx, i, "g");
}
if (type === "Table") html += "</table>";
return html;
};
//---------------------------------------------------------
// Load call items by name;
this.Load = function (callName) {
this.Data = {};
var basicTemplate = System.Security.Password.Templates.Calls.EN_Unicode;
var item;
var code;
var property;
// Copy properties from default template.
for (property in basicTemplate.Data) {
item = basicTemplate.Data[property];
code = eval(item[0]);
this.Data[code] = [];
this.Data[code][0] = item[1];
this.Data[code][1] = item[2];
}
// Copy properties from named template.
var template = System.Security.Password.Templates.Calls[callName];
if (typeof template === "object") {
// Overwrite/Append to Basic template.
this.Name = template.CallName;
for (property in template.Data) {
item = template.Data[property];
code = eval(item[0]);
this.Data[code] = [];
this.Data[code][0] = item[1];
this.Data[code][1] = item[2];
}
}
// Route thru combined template.
for (code in this.Data) {
this.Chars = {};
// If it is proper number then...
this.Chars[code] = new System.Security.Password.Char(code);
this.Chars[code].Html = this.ToHtml(this.Chars[code].Char);
}
};
//---------------------------------------------------------
this.InitializeClass = function () {
this.Load("Basic");
if (name) this.Load(name);
};
this.InitializeClass();
};
//=============================================================================
// CLASS: Security.Password.Presets
//-----------------------------------------------------------------------------
System.Security.Password.Presets = function (presetName) {
//---------------------------------------------------------
// Public properties.
this.Items;
//---------------------------------------------------------
// Private properties.
var me = this;
//---------------------------------------------------------
// Load all items from templates;
this.Load = function (name) {
// If name was specified then...
if (name) {
// Load single preset.
this.Items[property] = new System.Security.Password.Preset(property);
this.Items[property]["Parent"] = this;
} else {
// Load all presets.
for (var property in System.Security.Password.Templates.Presets) {
this.Items[property] = new System.Security.Password.Preset(property);
this.Items[property]["Parent"] = this;
}
}
}
//---------------------------------------------------------
this.InitializeClass = function () {
this.Items = {};
this.Load(presetName);
}
this.InitializeClass();
}
//=============================================================================
// CLASS: Security.Password.Preset
//-----------------------------------------------------------------------------
System.Security.Password.Preset = function (presetName) {
//---------------------------------------------------------
// Public properties.
this.Parent;
this.Name;
//---------------------------------------------------------
// Private properties.
var me = this;
//---------------------------------------------------------
// Load from template;
this.Load = function (presetName) {
this.Name = presetName;
// Try to get data from cache.
var tmp = System.Security.Password.Temp;
if (!tmp.Charsets) {
tmp.Charsets = new System.Security.Password.Templates.Charsets();
}
// Update default preset with proper chars from charsets object.
var p = System.Security.Password.Templates.Presets;
p.Default.CharsNumbers = tmp.Charsets.Strings.Numbers;
p.Default.CharsUppercase = tmp.Charsets.Strings.Uppercase;
p.Default.CharsLowercase = tmp.Charsets.Strings.Lowercase;
p.Default.CharsExtended = tmp.Charsets.Strings.Extended;
p.Default.CharsExtra = tmp.Charsets.Strings.Extra;
var property;
// Copy properties from default template.
for (property in p.Default) {
this[property] = p.Default[property];
}
// Apply properties from custom template.
for (property in p[presetName]) {
this[property] = p[presetName][property];
}
};
//---------------------------------------------------------
this.InitializeClass = function () {
if (presetName) {
this.Load(presetName);
} else {
// Load "Remember" preset by default;
this.Load("Remember");
}
};
this.InitializeClass();
};
//=============================================================================
// CLASS: Security.Password.Word
//-----------------------------------------------------------------------------
System.Security.Password.Word = function () {
this.Text = "";
this.Log = [];
this.Strength = 0;
this.ErrorMessage = "";
//---------------------------------------------------------
this.AppendLog = function (message) {
this.Log.push(message);
};
//---------------------------------------------------------
this.Reset = function () {
this.Text = "";
this.Strength = 0;
};
//---------------------------------------------------------
this.LogToHtml = function () {
var html = "";
for (var i = 0; i < this.Log.length; i++) {
html += this.Log[i] + "<br />";
}
return html;
};
//---------------------------------------------------------
this.InitializeClass = function () {
this.Reset();
};
this.InitializeClass();
};
//=============================================================================
// CLASS: Security.Password.Generator
//-----------------------------------------------------------------------------
System.Security.Password.Generator = function () {
/// <summary>
///
/// </summary>
/// <remarks>
/// Password is generated in these steps:
/// a) Load array of chars accorging to presets;
/// b) Apply filters;
/// c) Proceed until password length is reached;
/// d) Apply Regular Expressions;
/// </remarks>
//---------------------------------------------------------
// Private properties.
var me = this;
//---------------------------------------------------------
// Public properties.
this.PhoneKeys;
this.Keyboard;
this.Cache = {};
//---------------------------------------------------------
this.PresetChanged;
this.Error;
//---------------------------------------------------------
this.RiseEvent = function (e) { if (this[e.Name]) this[e.Name](this, e); };
//---------------------------------------------------------
this.Preset;
this.setPreset = function (preset) {
if (typeof preset === "string") {
preset = new System.Security.Password.Preset(preset);
}
this.Preset = preset;
this.RiseEvent("PresetChanged");
};
//---------------------------------------------------------
// Convert string to array.
this._stringToArray = function (stringText) {
var array = [];
var string = new String(stringText);
for (var i = 0; i < string.length; i++) {
array.push(string.charAt(i));
}
return array;
};
//---------------------------------------------------------
// Convert array to string.
this._arrayToString = function (array, splitter) {
var string = "";
for (var i = 0; i < array.length; i++) {
if (i > 0) string += splitter;
string += array[i];
}
};
//---------------------------------------------------------
this._splitString = function (string, splitter) {
var charsFilter = this._stringToArray(string);
var charsSplited = this._arrayToString(charsFilter, splitter);
return charsSplited;
};
//---------------------------------------------------------
this.NewPassword = function (cache) {
/// </summary>
/// Generate new password.
/// </summary>
/// <param name="cache" type="Bool">Enable cache</param>
// Create a new password item.
var password = new System.Security.Password.Word();
password.AppendLog("Password log started.");
// Maximum number of attempts to generate password.
var maxTries = 4;
for (var a = 1; a <= maxTries; a++) {
password.Reset();
password.AppendLog("Attempt " + a + " to generate password.");
if (!(this.Cache.charsToUse && cache)) {
this.Cache.charsToUse = this.GetChars();
}
var charsToUse = this.Cache.charsToUse;
if (charsToUse.length <= 0) {
password.AppendLog("Error: chars list is empty!");
} else {
password.AppendLog("Chars To Use: " + charsToUse);
// Now we can create our password.
for (var i = 0; i < this.Preset.PasswordLength; i++) {
// Apply filters and get list of available chars.
var filteredChars = this.FilterChars(password.Text, charsToUse);
// If filters left some chars then...
if (filteredChars.length > 0) {
// Calclulate password strenght (10^x).
var strength = Math.log(filteredChars.length) / Math.log(10);
password.Strength += strength;
// Get random char.
var charPosition = Math.floor(Math.random() * filteredChars.length);
var filteredChar = filteredChars.charAt(charPosition);
password.Text += filteredChar;
password.AppendLog(i + ". " + filteredChar + " <- [" + charPosition + "][" + filteredChars + "] - Final Strength: 10^" + password.Strength);
} else {
password.AppendLog("Warning: filteredChars.length == 0");
break;
}
}
}
// If we reached required password length then...
if (password.Text.length === this.Preset.PasswordLength) {
// Don't try to generate another password again.
break;
}
}
// Apply regular expressions to password.
password = this.ApplyRegex(password);
// Apply script to password.
password = this.ApplyScript(password);
// If password is empty then...
if (password.Text.length !== this.Preset.PasswordLength) {
// Add error message.
password.AppendLog("Error: all attempts to generate password was failed. Please relax password settings.");
}
return password;
};
//-------------------------------------------------------
this.GetChars = function (preset) {
/// <summary>
/// Create chars string by preset.
/// </summary>
var charsToUse = "";
for (var property in System.Security.Password.CharTypeEnum) {
var enabled = this.Preset["Use" + property];
var ratio = this.Preset["Ratio" + property];
if (enabled && ratio > 0) {
var chars = this.Preset["Chars" + property];
for (var i = 0; i < ratio; i++) {
charsToUse += chars;
}
}
}
return charsToUse;
};
//-------------------------------------------------------
this.FilterChars = function (password, chars) {
var charsToUse = chars;
for (var property in System.Security.Password.FilterTypeEnum) {
var enabled = this.Preset["Filter" + property];
if (enabled) {
charsToUse = System.Security.Password.Templates.Filters[property](this, password, charsToUse);
}
}
return charsToUse;
};
//-------------------------------------------------------
this.ApplyScript = function (password) {
if (this.Preset.ScriptEnabled) {
password.AppendLog("ApplyScript to '" + password.Text + "'");
try {
if (this.Preset.ScriptCode.length > 0) {
var fn = new Function("password", this.Preset.ScriptCode);
var text = fn(password);
password.Text = text;
password.AppendLog("password.Text = '" + password.Text + "'");
}
} catch (ex) {
var msg = "Error: ApplyScript: " + ex.message;
password.AppendLog(msg);
password.ErrorMessage = msg;
// Rise error here.
var e = new System.EventArgs(Error);
e["Message"] = msg;
this.RiseEvent(e);
}
}
return password;
};
//-------------------------------------------------------
this.ApplyRegex = function (password) {
if (this.Preset.RegexEnabled) {
password.AppendLog("ApplyRegex to '" + password.Text + "'");
try {
var ic = this.Preset.RegexIgnoreCase ? "i" : "";
var pf = new RegExp(this.Preset.RegexPatternFind, ic);
password.Text = password.Text.replace(pf, this.Preset.RegexPatternReplace);
password.AppendLog("password.Text = '" + password.Text + "'");
} catch (ex) {
password.AppendLog("Error: ApplyRegex: " + ex.message);
password.Text = password;
// rise error here.
}
}
return password;
};
//---------------------------------------------------------
this.InitializeClass = function () {
// Preconfigure generator with default preset;
this.setPreset("DefaultEasyToRemember");
};
this.InitializeClass.apply(this, arguments);
};
//=============================================================================
// NameSPACE: Security.Password.Templates
//-----------------------------------------------------------------------------
System.Security.Password.Templates = {};
//=============================================================================
// NameSPACE: Security.Password.Templates.Presets
//-----------------------------------------------------------------------------
System.Security.Password.Templates.Presets = {};
System.Security.Password.Templates.Presets.Default = {
"PresetName": "Default",
"PresetDescription": "",
"PresetRemarks": "",
"PasswordLength": 9,
"UseNumbers": false,
"UseUppercase": false,
"UseLowercase": false,
"UseExtended": false,
"UseExtra": false,
"RatioNumbers": 1,
"RatioUppercase": 1,
"RatioLowercase": 1,
"RatioExtended": 1,
"RatioExtra": 1,
"CharsNumbers": "",
"CharsUppercase": "",
"CharsLowercase": "",
"CharsExtended": "",
"CharsExtra": "",
"FilterRemember": false,
"FilterKeyboard": false,
"FilterPhone": false,
"FilterAscii": false,
"FilterChars": false,
"FilterCharsString": "",
"ScriptEnabled": false,
"ScriptCode": "",
"RegexEnabled": false,
"RegexPatternFind": "",
"RegexPatternReplace": "",
"RegexIgnoreCase": false
};
System.Security.Password.Templates.Presets.DefaultCrazy = {
"PresetName": "Default: Crazy",
"RatioNumbers": 3,
"RatioLowercase": 3,
"RatioUppercase": 3,
"RatioExtended": 1,
"UseNumbers": true,
"UseLowercase": true,
"UseUppercase": true,
"UseExtended": true,
"PasswordLength": 10
};
/*
System.Security.Password.Templates.Presets.DefaultEasyForLeftHand = {
PresetName: "Default: Easy For Left Hand",
RatioNumbers: 1,
RatioLowercase: 2,
UseNumbers: true,
UseLowercase: true,
FilterChars: true,
FilterCharsString: "7890yuiophjklnmYUIOPHJKLNM-=[];'#,./()_+{}:@~<>\?"
}
System.Security.Password.Templates.Presets.DefaultEasyForRightHand = {
PresetName: "Default: Easy For Right Hand",
RatioNumbers: 1,
RatioLowercase: 2,
UseNumbers: true,
UseLowercase: true,
FilterChars: true,
FilterCharsString: "123456qwertasdfgzxcvbQWERTASDFGZXCVB\\`¬!\"£$%^|"
}
*/
System.Security.Password.Templates.Presets.DefaultPhone = {
"PresetName": "Default: GSM Phone",
"RatioLowercase": 1,
"UseLowercase": true,
"FilterPhone": true
};
System.Security.Password.Templates.Presets.DefaultEasyToEnter = {
"PresetName": "Default: Easy To Enter",
"RatioLowercase": 1,
"UseLowercase": true,
"FilterChars": true,
"FilterCharsString": "qwxzQWXZ",
"FilterKeyboard": true
};
System.Security.Password.Templates.Presets.DefaultEasyToRemember = {
"PresetName": "Default: Easy To Remember",
"RatioLowercase": 1,
"FilterRemember": true,
"UseLowercase": true,
"FilterChars": true,
"FilterCharsString": "qwxzQWXZ"
};
System.Security.Password.Templates.Presets.DefaultGood = {
"PresetName": "Default: Good",
"RatioNumbers": 1,
"RatioLowercase": 1,
"RatioUppercase": 1,
"UseNumbers": true,
"UseLowercase": true,
"UseUppercase": true,
"PasswordLength": 10
};
System.Security.Password.Templates.Presets.DefaultLinuxManiac = {
"PresetName": "Default: Linux Maniac",
"RatioNumbers": 1,
"RatioLowercase": 1,
"RatioUppercase": 1,
"RatioExtended": 1,
"RatioExtra": 16,
"UseNumbers": true,
"UseLowercase": true,
"UseUppercase": true,
"UseExtended": true,
"UseExtra": false,
"PasswordLength": 12
};
System.Security.Password.Templates.Presets.DefaultNormal = {
"PresetName": "Default: Normal",
"RatioNumbers": 1,
"RatioLowercase": 1,
"UseNumbers": true,
"UseLowercase": true
};
System.Security.Password.Templates.Presets.NumBinary = {
"PresetName": "Num: Binary",
"CharsExtra": "01",
"RatioExtra": 1,
"UseExtra": true,
"PasswordLength": 10
};
System.Security.Password.Templates.Presets.NumOctet = {
"PresetName": "Num: Octet",
"CharsExtra": "01234567",
"RatioExtra": 1,
"UseExtra": true,
"PasswordLength": 10
};
System.Security.Password.Templates.Presets.DecCodePASS = {
"PresetName": "Dec: Code - PASS",
"RatioNumbers": 1,
"UseNumbers": true,
"PasswordLength": 8
};
System.Security.Password.Templates.Presets.DecCodePIN = {
"PresetName": "Dec: Code - PIN",
"RatioNumbers": 1,
"UseNumbers": true,
"PasswordLength": 4
};
System.Security.Password.Templates.Presets.DecDefault = {
"PresetName": "Dec: Default",
"RatioNumbers": 1,
"UseNumbers": true,
"PasswordLength": 10
};
System.Security.Password.Templates.Presets.DecIMEI = {
"PresetName": "Dec: IMEI - Mobile ID",
"RatioNumbers": 1,
"UseNumbers": true,
"PasswordLength": 15
};
System.Security.Password.Templates.Presets.GuidDefault = {
"PresetName": "Hex: GUID - Default",
"CharsExtra": "abcdef",
"RatioNumbers": 1,
"RatioExtra": 1,
"UseNumbers": true,
"UseExtra": true,
"PasswordLength": 32,
"RegexEnabled": true,
"RegexPatternFind": "(\\S{8})(\\S{4})(\\S{4})(\\S{4})(\\S{12})",
"RegexPatternReplace": "$1-$2-$3-$4-$5"
};
System.Security.Password.Templates.Presets.GuidAccess = {
"PresetName": "Hex: GUID - Access",
"CharsExtra": "abcdef",
"RatioNumbers": 1,
"RatioExtra": 1,
"UseNumbers": true,
"UseExtra": true,
"PasswordLength": 32,
"RegexEnabled": true,
"RegexPatternFind": "(\\S{8})(\\S{4})(\\S{4})(\\S{4})(\\S{12})",
"RegexPatternReplace": "{$1-$2-$3-$4-$5}"
};
System.Security.Password.Templates.Presets.GuidPlain = {
"PresetName": "Hex: GUID - Plain",
"CharsExtra": "abcdef",
"RatioNumbers": 1,
"RatioExtra": 1,
"UseNumbers": true,
"UseExtra": true,
"PasswordLength": 32
};
System.Security.Password.Templates.Presets.HexHEXLowercase = {
"PresetName": "Hex: HEX - Lowercase",
"CharsExtra": "abcdef",
"RatioNumbers": 1,
"RatioExtra": 1,
"UseNumbers": true,
"UseExtra": true,
"PasswordLength": 10
};
System.Security.Password.Templates.Presets.HexHEXUppercase = {
"PresetName": "Hex: HEX - Uppercase",
"CharsExtra": "ABCDEF",
"RatioNumbers": 1,
"RatioExtra": 1,
"UseNumbers": true,
"UseExtra": true,
"PasswordLength": 10
};
System.Security.Password.Templates.Presets.HexMAC = {
"PresetName": "Hex: MAC",
"CharsExtra": "ABCDEF",
"RatioNumbers": 1,
"RatioExtra": 1,
"UseNumbers": true,
"UseExtra": true,
"PasswordLength": 12,
"RegexEnabled": true,
"RegexPatternFind": "(\\S{2})(\\S{2})(\\S{2})(\\S{2})(\\S{2})(\\S{2}).*",
"RegexPatternReplace": "$1-$2-$3-$4-$5-$6"
};
System.Security.Password.Templates.Presets.HexMD5 = {
"PresetName": "Hex: MD5",
"CharsExtra": "ABCDEF",
"RatioNumbers": 1,
"RatioExtra": 1,
"UseNumbers": true,
"UseExtra": true,
"PasswordLength": 32
};
System.Security.Password.Templates.Presets.OtherDragon = {
"PresetName": "Other: Dragon",
"RatioNumbers": 1,
"RatioLowercase": 2,
"RatioUppercase": 2,
"UseNumbers": true,
"UseLowercase": true,
"UseUppercase": true,
"PasswordLength": 8,
"RegexEnabled": true,