生成随机密码的函数

14
我想使用下面的函数为员工生成随机密码。这是我在R中编写函数的第一次尝试,所以我需要一些帮助。
genPsw <- function(num, len=8) {
          # Vorgaben für die Passwortkonventionen festlegen
            myArr  <- c("", 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", 
                        "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", 
                        "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", 
                        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
                        "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", 
                        "!", "§", "$", "%", "&", "(", ")", "*")
          # replicate is a wrapper for the common use of sapply for repeated evaluation of an expression 
          # (which will usually involve random number generation).
            replicate(num, paste(sample(myArr, size=len, replace=T), collapse=""))
          # nrow of dataframe mitarbeiter 
          dim_mitarbeiter <- nrow(mitarbeiter)
          for(i in 1:dim_mitarbeiter) {
                        # Random Number Generation with i
                          set.seed(i)
                        # Generate Passwort for new variable password
                        mitarbeiter$passwort <- genPsw(i)                
          }

}

根据Floo0的答案,我已经将函数更改为以下内容,但它并不起作用:

genPsw <- function(num, len=8) {
          # Vorgaben für die Passwortkonventionen festlegen
          sam<-list()
          sam[[1]]<-1:9
          sam[[2]]<-letters
          sam[[3]]<-LETTERS
          sam[[4]]<-c("!", "§", "$", "%", "&", "(", ")", "*")

          # nrow of dataframe mitarbeiter 
          dim_mitarbeiter <- nrow(mitarbeiter)
          for(i in 1:dim_mitarbeiter) {
                        # Random Number Generation with i
                            tmp<-mapply(sample,sam,c(2,2,2,2))
                         # Generate Passwort for new variable password
                        mitarbeiter$passwort <- paste(sample(tmp),collapse="")
          }

}

相关问题:R中的密码生成器函数 - zx8754
1
为了简化myArr,你可以使用R常量 - LETTERSletters - zx8754
1
给他们全部设置为“correcthorsebatterystaple”。 - Carl Witthoft
7个回答

17

怎么样呢?

samp<-c(2:9,letters,LETTERS,"!", "§", "$", "%", "&", "(", ")", "*")
paste(sample(samp,8),collapse="")

结果大致如此

"HKF§VvnD"

注意:该方法不强制使用大写字母、数字和非字母数字符号。

编辑:

如果你想强制使用一定数量的大写字母、数字和非字母数字符号,可以尝试以下方法:

sam<-list()
sam[[1]]<-1:9
sam[[2]]<-letters
sam[[3]]<-LETTERS
sam[[4]]<-c("!", "§", "$", "%", "&", "(", ")", "*")

tmp<-mapply(sample,sam,c(2,2,2,2))
paste(sample(tmp),collapse="")

其中 c(2,2,2,2) 指定数字、字母、大写字母和符号的数量(按此顺序)。结果:

[1] "j$bP%5R3"

编辑2:要在您的表mitarbeiter中创建一个新列,只需使用以下命令:

passwort<-replicate(nrow(mitarbeiter),paste(mapply(sample,sam,c(2,2,2,2)),collapse=""))
mitarbeiter$passwort<-passwort

1
你可以通过将 c(2,2,2,2) 替换为从 1:5 随机选择的值,重复三次并分配第四个值 > 1 来扩展此内容,以达到所需的密码长度。 - Carl Witthoft
我能否将其放入一个函数中,如下所示: dim_mitarbeiter <- nrow(mitarbeiter) for (i in 1:dim_mitarbeiter) { sam<-list() sam[[1]]<-1:9 sam[[2]]<-letters sam[[3]]<-LETTERS sam[[4]]<-c("!", "§", "$", "%", "&", "(", ")", "*") tmp<-mapply(sample,sam,c(2,2,2,2)) mitarbeiter$passwort <- tmp } - SebastianS
只需使用passwort<-replicate(nrow(mitarbeiter),paste(mapply(sample,sam,c(2,2,2,2)),collapse="")),然后mitarbeiter$passwort<-passwort - Rentrop

7

stringi 包中有一个生成随机字符串的函数:

require(stringi)
stri_rand_strings(n=2, length=8, pattern="[A-Za-z0-9]")
## [1] "90i6RdzU" "UAkSVCEa"

1
stringi能否生成包含§$%(可打印ASCII字符)的字符串? - ECII
asc.read <- c(33L, 34L, 35L, 36L, 37L, 38L, 40L, 41L, 42L, 43L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 93L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 125L)rawToChar(as.raw(asc.read)) - ECII

3

这可能有效,但人们可能想要更改ASCII以避免不必要的符号:

generatePwd <- function(plength=8, ASCII=c(33:126)) paste(sapply(sample(ASCII, plength), function(x) rawToChar(as.raw(x))), collapse="")

你可能想用 ´sample(ASCII, plength, replace=T)´ 替换 ´sample(ASCII, plength)´。 - ECII

2
下面的脚本可以创建一个指定长度的密码,由大写字母、小写字母、数字和32个符号(标点等)组成。
# Store symbols as a vector in variable punc
R> library(magrittr) # Load this package to use the %>% (pipe) operator
R> punc_chr <- "!#$%&’()*+,-./:;<=>?@[]^_`{|}~" %>%
     str_split("", simplify = TRUE) %>%
     as.vector() -> punc

# Randomly generate specified number of characters from 94 characters
R> sample(c(LETTERS, letters, 0:9, punc), 8) %>%
     paste(collapse = "") -> pw
R> pw # Return password
[1] "fAXVpyOs"

0

我喜欢L.R.解决方案的简洁性,尽管我没有完全理解它的功能。

我的解决方案允许指定密码的长度,但也确保至少包含一个小写字母、一个大写字母、一个数字和一个特殊字符,并允许可重复性。(感谢moazzem列出所有特殊字符。)

gen_pass <- function(len=8,seeder=NULL){
  set.seed(seeder) # to allow replicability of passw generation
 # get all combinations of 4 nums summing to length len
  all_combs <- expand.grid(1:(len-3),1:(len-3),1:(len-3),1:(len-3))
  sum_combs <- all_combs[apply(all_combs, 1, function(x) sum(x)==len),]
 # special character vector
  punc <- unlist(strsplit("!#$%&’()*+,-./:;<=>?@[]^_`{|}~",""))
 # list of all characters to sample from
  chars <- list(punc,LETTERS,0:9,letters)
 # retrieve the number of characters from each list element 
 # specified in the sampled row of sum_combs
  pass_chars_l<- mapply(sample, chars,
                        sum_combs[sample(1:nrow(sum_combs),1),],
                        replace = TRUE)
 # unlist sets of password characters 
  pass_chars <- unlist(pass_chars_l)
 # jumble characters and combine into password 
  passw <- str_c(sample(pass_chars),collapse = "")
 return(passw)
}

我仍在想如何更优雅地表达expand.grid(1:(len-3),1:(len-3),1:(len-3),1:(len-3))中的(1:(len-3),1:(len-3),1:(len-3),1:(len-3))


0

我刚刚尝试了Fmerhout提出的函数,它看起来是一个很好的解决方案。非常感谢。但是由于代码的最后一行:

    passw <- str_c (sample (pass_chars), collapse = "")    

这个函数不起作用。 我尝试过:

    passw <- str (sample (pass_chars), collapse = "")     

......现在它可以工作了:

带有种子的示例:

    >gen_pass(4,2)

给出:chr [1:4] "y" "[" "O" "1"

为了获得可直接使用的密码,我将代码结尾改成了这样:

    zz <- sample(pass_chars)
    passw <- paste(zz, sep = "", collapse = "")
    return(passw)
    }

所以,现在我们举个例子:

    > gen_pass(35, 2)
    [1] "0OD}1O}8DKMqTL[JEFZBwKMJWGD’VZ=VRnD"

很有趣;因为我们只需要记住传递给函数的参数。在这种情况下:35 2。 谢谢您,Fmerhout先生。

总之:通过这个小脚本,我们有一个很好的方法来创建非常强壮和非常安全的密码,具有良好的熵值,而无需使用字典,也不必在任何地方记录我们的密码。


0

这将生成随机的小写字母和数字,但如果你愿意,你可以在其中嵌套另一个 paste 函数,并使用一组特殊字符/大写字母。

paste0(paste(sample(letters, 7), collapse = ""), paste(sample(99999, 1)))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接