Linux – 根据要求生成随机密码

huangapple go评论71阅读模式
英文:

Linux - Generate random password based on requirements

问题

你好,
我已经搜索了一整天,但无法找到在Bash中生成具体密码要求的方法:

  • 至少12个字符长度;
  • 至少4个不同的字母;
  • 至少3个数字;
  • 至少3个特殊字符 -_+=#^*

我使用了urandom:tr -dc A-Za-z0-9_ < /dev/urandom | head -c 16 | xargs,它会生成一个包含字母、数字和下划线的16个字符字符串。但是我无法指定上述的标准。

英文:

\Hello,
I've been searching for a day but cannot find a way to specify concrete password requirements when generating one with Bash:<br>
-at least 12 char length:<br>
-at least 4 different letters:<br>
-at least 3 digits:<br>
-at least 3 special chars -_+=#^*

I use the urandom: tr -dc A-Za-z0-9_ &lt; /dev/urandom | head -c 16 | xargs which generates, in this case, 16 char string with letters, numbers and underscore. But I cannot specify the critera above.

答案1

得分: 1

以下是翻译好的内容:

例如,这将满足标准:

echo $(tr -dc A-Za-z < /dev/urandom | head -c 16)abcd123+-=

第一个标准很容易,你已经做到了,其他的标准是通过在末尾附加固定后缀来实现的。

你可以通过对结果进行随机排列来稍微改进它:

echo $(echo $(tr -dc A-Za-z < /dev/urandom | head -c 16)abcd123+-= | fold -w 1 | shuf | tr -d "\n")

(我借助了这个答案

英文:

For example this would fulfill criteria:

echo $(tr -dc A-Za-z &lt; /dev/urandom | head -c 16)abcd123+-=

First criterium is easy and you already achieved that, others are fulfilled by fixed suffix appended to end.

You can make it bit better by random permutation of the result:

echo $(echo $(tr -dc A-Za-z &lt; /dev/urandom | head -c 16)abcd123+-= | fold -w 1 | shuf | tr -d &quot;\n&quot;)

(I helped myself with this answer)

答案2

得分: 0

以下是您要翻译的内容:

Solution in TXR Lisp:

$ txr pwgen.tl
ix8K+6I6^#hp
$ txr pwgen.tl
s#J_&amp;Eo348wl
$ txr pwgen.tl
dKt7V4#g&amp;c9+

Code:

(let* ((abc (join &quot;a&quot;..&quot;z&quot;))
       (ABC (upcase-str abc))
       (dig (join &quot;0&quot;..&quot;9&quot;))
       (chr &quot;-_+=#^&amp;&quot;))
  (labels ((relem (str) [str (rand (len str))])
           (dr (str n) [(shuffle str) 0..n])
           (r (str n) (str-seq (take n (gun (relem str)))))))
    (flow `@(dr abc 4)@(r dig 3)@(r chr 3)@(r ABC 2)`
      shuffle
      put-line)))

We must call (make-random-state) to make a new, randomized state for the pseudo-random-number generator (PRNG), and store that in *random-state*. Otherwise we will get the same password every time.

The rules about which chararacters must be present give us ten characters. So we need two more. Let us meet that requirement by choosing two random upper case letters.

The program prepares some strings: abc contains the alphabet, dig the digits 0 through 9 and so on.

There are some local functions: (relem str) chooses a random character from the string str; (dr str n) chooses n different random ("dr") characters from str and returns them as a string; (r str n) chooses n random characters from str, not necessarily different.

We then use these functions inside a quasistring: a back-quoted string template with interpolation. That gives us a string in which the required items are in order: four different letters, three digits, three characters from the set, and then two random upper case letters.

We inject this string into a pipeline using flow which passes it thorugh shuffle, and hands it off to put-line.

(gun (relem str)) means repeatedly evaluate the expression (relem str) and use the values to generate an lazy sequence, until that expression returns nil: ("gun" means "generate until nil"). Since relem doesn't return nil, we get an infinite sequence of random choices of letters from str. We use (take n (gun ...)) to just take the first n, and then str-seq (string from sequence) to turn those letters into a string.

英文:

Solution in TXR Lisp:

$ txr pwgen.tl
ix8K+6I6^#hp
$ txr pwgen.tl
s#J_&amp;Eo348wl
$ txr pwgen.tl
dKt7V4#g&amp;c9+

Code:

(let* ((abc (join &quot;a&quot;..&quot;z&quot;))
       (ABC (upcase-str abc))
       (dig (join &quot;0&quot;..&quot;9&quot;))
       (chr &quot;-_+=#^&amp;&quot;))
  (labels ((relem (str) [str (rand (len str))])
           (dr (str n) [(shuffle str) 0..n])
           (r (str n) (str-seq (take n (gun (relem str))))))
    (flow `@(dr abc 4)@(r dig 3)@(r chr 3)@(r ABC 2)`
      shuffle
      put-line)))

We must call (make-random-state) to make a new, randomized state for the pseudo-random-number generator (PRNG), and store that in *random-state*. Otherwise we will get the same password every time.

The rules about which chararacters must be present give us ten characters. So we need two more. Let us meet that requirement by choosing two random upper case letters.

The program prepares some strings: abc contains the alphabet, dig the digits 0 through 9 and so on.

There are some local functions: (relem str) chooses a random character from the string str; (dr str n) chooses n different random ("dr") characters from str and returns them as a string; (r str n) chooses n random characters from str, not necessarily different.

We then use these functions inside a quasistring: a back-quoted string template with interpolation. That gives us a string in which the required items are in order: four different letters, three digits, three characters from the set, and then two random upper case letters.

We inject this string into a pipeline using flow which passes it thorugh shuffle, and hands it off to put-line.

(gun (relem str)) means repeatedly evaluate the expression (relem str) and use the values to generate an lazy sequence, until that expression returns nil: ("gun" means "generate until nil"). Since relem doesn't return nil, we get an infinite sequence of random choices of letters from str. We use (take n (gun ...)) to just take the first n, and then str-seq (string from sequence) to turn those letters into a string.

huangapple
  • 本文由 发表于 2023年7月13日 18:16:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定