什么是创建一个包含8个字符的随机密码的最佳方法a-z
,A-Z
以及0-9
?
绝对没有安全问题,这只是用于原型设计,我只想要看起来真实的数据.
我在考虑for (0 to 7) Math.random
生成ASCII码并将它们转换为字符.你有什么其他的建议?
Gum*_*mbo 134
我可能会使用这样的东西:
function generatePassword() {
var length = 8,
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
retVal = "";
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
然后可以将其扩展为使参数传递长度和字符集.
Aro*_*olm 34
这是我所知道的最快捷,最简单的方法:
Math.random().toString(36).slice(2)
Run Code Online (Sandbox Code Playgroud)
我们的想法是将一个随机数(在0..1范围内)转换为base36字符串(小写字母az加0-9)并删除前导零和小数点.
请注意,伪生成的所有密码都存在某种形式的漏洞.但是,我会说它对所有正常使用情况都足够好.此外,如果不保证此密码,理论长度.最多16个字符,最少0个字符.当循环运行100万次时,其平均长度为15.67个字符,最小长度为5.但是,如果将其中两个密码连接在一起,则最大长度为32个字符,平均长度为31.33个字符和一个最小长度20.
Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)
Run Code Online (Sandbox Code Playgroud)
我个人bookmarklet
在浏览器书签栏中使用它作为chrome 来快速生成密码:
javascript:(
function(){
prompt('Here is your shiny new password:',
Math.random().toString(36).slice(2) +
Math.random().toString(36).slice(2)
);
}
)();
Run Code Online (Sandbox Code Playgroud)
haj*_*ist 25
function password_generator( len ) {
var length = (len)?(len):(10);
var string = "abcdefghijklmnopqrstuvwxyz"; //to upper
var numeric = '0123456789';
var punctuation = '!@#$%^&*()_+~`|}{[]\:;?><,./-=';
var password = "";
var character = "";
var crunch = true;
while( password.length<length ) {
entity1 = Math.ceil(string.length * Math.random()*Math.random());
entity2 = Math.ceil(numeric.length * Math.random()*Math.random());
entity3 = Math.ceil(punctuation.length * Math.random()*Math.random());
hold = string.charAt( entity1 );
hold = (password.length%2==0)?(hold.toUpperCase()):(hold);
character += hold;
character += numeric.charAt( entity2 );
character += punctuation.charAt( entity3 );
password = character;
}
password=password.split('').sort(function(){return 0.5-Math.random()}).join('');
return password.substr(0,len);
}
console.log( password_generator() );
Run Code Online (Sandbox Code Playgroud)
这会生成一个更强大的密码,应该通过任何密码强度测试.如:f1&d2?I4(h1&
,C1^y1)j1@G2#
,j2{h6%b5@R2)
Sum*_*wal 12
function generatePass(plength){
var keylistalpha="abcdefghijklmnopqrstuvwxyz";
var keylistint="123456789";
var keylistspec="!@#_";
var temp='';
var len = plength/2;
var len = len - 1;
var lenspec = plength-len-len;
for (i=0;i<len;i++)
temp+=keylistalpha.charAt(Math.floor(Math.random()*keylistalpha.length));
for (i=0;i<lenspec;i++)
temp+=keylistspec.charAt(Math.floor(Math.random()*keylistspec.length));
for (i=0;i<len;i++)
temp+=keylistint.charAt(Math.floor(Math.random()*keylistint.length));
temp=temp.split('').sort(function(){return 0.5-Math.random()}).join('');
return temp;
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*ati 11
code 用于生成给定长度(默认为 8)的密码,并且至少包含一个大写字母、一个小写字母、一个数字和一个符号
(2 个函数和一个名为“Allowed”的 const 变量)
const Allowed = {
Uppers: "QWERTYUIOPASDFGHJKLZXCVBNM",
Lowers: "qwertyuiopasdfghjklzxcvbnm",
Numbers: "1234567890",
Symbols: "!@#$%^&*"
}
const getRandomCharFromString = (str) => str.charAt(Math.floor(crypto.randomInt(0, str.length)))
/**
* the generated password will be @param length, which default to 8,
* and will have at least one upper, one lower, one number and one symbol
* @param {number} length - password's length
* @returns a generated password
*/
const generatePassword = (length = 8) => {
let pwd = "";
pwd += getRandomCharFromString(Allowed.Uppers); // pwd will have at least one upper
pwd += getRandomCharFromString(Allowed.Lowers); // pwd will have at least one lower
pwd += getRandomCharFromString(Allowed.Numbers); // pwd will have at least one number
pwd += getRandomCharFromString(Allowed.Symbols); // pwd will have at least one symbol
for (let i = pwd.length; i < length; i++)
pwd += getRandomCharFromString(Object.values(Allowed).join('')); // fill the rest of the pwd with random characters
return pwd
}
Run Code Online (Sandbox Code Playgroud)
并记住导入crypto
(内置JS):
import * as crypto from "crypto";
// or
import crypto from "crypto";
// or
const crypto = require("crypto");
Run Code Online (Sandbox Code Playgroud)
Math.random
为crypto.randomInt
(:我了解到这Math.random
是不安全的 - 这意味着它可以很容易地被“黑客攻击”,这意味着密码可以通过某种方式被猜到。
我已经更新了上面的代码
这是我生成一个8个字符的加密随机密码的函数:
function generatePassword() {
var buf = new Uint8Array(6);
window.crypto.getRandomValues(buf);
return btoa(String.fromCharCode.apply(null, buf));
}
Run Code Online (Sandbox Code Playgroud)
它的作用:检索6个加密随机8位整数,并用Base64对它们进行编码.
由于结果是为Base64字符集所生成的密码可以由A
- Z
,a
- z
,0
- 9
,+
和/
.
请注意依赖的答案Math.random
- 它们并不安全。Math.random
这是一个老问题,所以仍然会出现也就不足为奇了,但是您绝对不应该使用它来生成字符串来保护任何内容。如果您确实需要支持早于 IE11 的浏览器,则应该添加一个后备以从后端获取使用CSPRNG生成的随机值。
function generatePassword(length) {
const crypto = window.crypto || window.msCrypto;
if (typeof crypto === 'undefined') {
throw new Error('Crypto API is not supported. Please upgrade your web browser');
}
const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const indexes = crypto.getRandomValues(new Uint32Array(length));
let secret = '';
for (const index of indexes) {
secret += charset[index % charset.length];
}
return secret;
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的例子。您可能想要向集合中添加特殊字符,并且可能强制存在数字或符号。
这是我的看法(使用 Typescript),使用浏览器加密 API 并强制执行至少具有以下内容的密码:
\nconst LOWER_CASE_CHARS = 'abcdefghijklmnopqrstuvwxyz'.split('');\nconst UPPER_CASE_CHARS = LOWER_CASE_CHARS.map((x) => x.toUpperCase());\nconst SYMBOLS = '!\xc2\xa3$%^&*()@~:;,./?{}=-_'.split('');\nconst LETTERS_MIX = [...LOWER_CASE_CHARS, ...UPPER_CASE_CHARS, ...SYMBOLS];\nconst CHARS_LENGTH = LETTERS_MIX.length;\n\nfunction containsLowerCase(str: string): boolean {\n return LOWER_CASE_CHARS.some((x) => str.includes(x));\n}\n\nfunction containsUpperCase(str: string): boolean {\n return UPPER_CASE_CHARS.some((x) => str.includes(x));\n}\n\nfunction containsSymbol(str: string): boolean {\n return SYMBOLS.some((x) => str.includes(x));\n}\n\nfunction isValidPassword(password: string) {\n return containsLowerCase(password) && containsUpperCase(password) && containsSymbol(password);\n}\n\nexport function generateStrongPassword(length: number = 16): string {\n const buff = new Uint8Array(length);\n\n let generatedPassword = '';\n\n do {\n window.crypto.getRandomValues(buff);\n generatedPassword = [...buff].map((x) => LETTERS_MIX[x % CHARS_LENGTH]).join('');\n } while (!isValidPassword(generatedPassword));\n\n return generatedPassword;\n}\n
Run Code Online (Sandbox Code Playgroud)\n
如果你有lodash> = 4.0,那么有一种更优雅的方式
var chars = 'abcdefghkmnpqrstuvwxyz23456789';
function generatePassword(length) {
return _.sampleSize(chars, length).join('');
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
70888 次 |
最近记录: |