Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Clojure中生成固定长度的随机密码?_Clojure - Fatal编程技术网

如何在Clojure中生成固定长度的随机密码?

如何在Clojure中生成固定长度的随机密码?,clojure,Clojure,有没有一种快速生成固定长度随机密码的方法?(例如,8位数字、字母/数字/下划线) 它不是确定性的,因为rand nth有副作用,但它应该足以让您开始使用。您可能需要使用crypto random库。它由Compojure的同一作者编写,适用于加密目的 user> (defn fixed-length-password ([] (fixed-length-password 8)) ([n] (let [chars (map char (r

有没有一种快速生成固定长度随机密码的方法?(例如,8位数字、字母/数字/下划线)


它不是确定性的,因为rand nth有副作用,但它应该足以让您开始使用。

您可能需要使用
crypto random
库。它由
Compojure
的同一作者编写,适用于加密目的

user> (defn fixed-length-password
        ([] (fixed-length-password 8))
        ([n]
           (let [chars (map char (range 33 127))
                 password (take n (repeatedly #(rand-nth chars)))]
             (reduce str password))))      
#'user/fixed-length-password
user> (fixed-length-password 10)
;=> "N&L[yyLUI4"
user> (fixed-length-password 10)
;-> "8JSF-:?si."
user> (fixed-length-password 10)
;=> "EbKS~?*J*h"
请注意,它将Java库包装在引擎盖下(
Java.security
apachecommons
。看看代码吧!它太小了

特别是在您的情况下,您可能正在寻找
hex
函数,因此解决方案是:
(crypto.random/hex size)


这是runexec答案的一个细微变化,在这里您可以看到如何选择随机字符串应该使用的字符。

也许
(defn rand string[chars n](>#(rand nth(vec chars))(重复n)(apply str))
vec
可以确保快速查找。\(反斜杠)问题:每当字符串中出现反斜杠时,它都会出现两次。这可能是由于Clojure的内部数据结构造成的。如何避免它?@Nick尝试以下方法(defn定长密码([](定长密码8))([n](let[chars(map char(range 33 127))password)(重复使用n次#(rand nth chars)))传递str(reduce str password)](clojure.string/replace pass str“\\\”“/”)这很酷。你能解释一下
(replace pass str“\\\”“/”)是怎么做的吗
work?为什么要用一个正斜杠替换双反斜杠?谢谢!没问题。请查看Clojure备忘单,查找字符文字=>使用
apply
而不是
reduce
将更快(特别是在较长的字符串上)。如果不在每次运行时计算
chars
,而只是将其绑定到全局值(函数外部),则只在编译时计算一次,则速度也会快得多。
user> (defn fixed-length-password
        ([] (fixed-length-password 8))
        ([n]
           (let [chars (map char (range 33 127))
                 password (take n (repeatedly #(rand-nth chars)))]
             (reduce str password))))      
#'user/fixed-length-password
user> (fixed-length-password 10)
;=> "N&L[yyLUI4"
user> (fixed-length-password 10)
;-> "8JSF-:?si."
user> (fixed-length-password 10)
;=> "EbKS~?*J*h"
user> (defn fixed-length-password
        ([] (fixed-length-password 8))
        ([n]
           (let [chars-between #(map char (range (int %1) (inc (int %2))))
                 chars (concat (chars-between \0 \9)
                               (chars-between \a \z)
                               (chars-between \A \Z)
                               [\_])
                 password (take n (repeatedly #(rand-nth chars)))]
             (reduce str password))))      
#'user/fixed-length-password
user> (fixed-length-password 10)
;=> "Pfm0hwppMr"
user> (fixed-length-password 10)
;-> "n6lQoz_KGd"
user> (fixed-length-password 10)
;=> "vCkubQR75Z"