简体   繁体   English

R中的密码生成器函数

[英]Password generator function in R

I am looking for a smart way to code a password generator function in R:我正在寻找一种在 R 中编写密码生成器函数的智能方法:

generate.password (length, capitals, numbers)
  • length: the length of the password length:密码的长度
  • capitals: a vector of defining where capitals shall occur, vector reflects the corresponsing password string position, default should be no capitals大写字母:定义大写字母出现的向量,向量反映对应的密码字符串位置,默认为无大写字母
  • numbers: a vector defining where capitals shall occur, vector reflects the corresponsing password string position, default should be no numbers数字:定义大写字母出现位置的向量,向量反映相应的密码字符串位置,默认应为无数字

Examples:例子:

generate.password(8)
[1] "hqbfpozr"


generate.password(length=8, capitals=c(2,4))
[1] "hYbFpozr"


generate.password(length=8, capitals=c(2,4), numbers=c(7:8))
[1] "hYbFpo49"

There is function which generates random strings in the stringi (version >= 0.2-3) package:stringi (version >= 0.2-3) 包中有一个生成随机字符串的函数:

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

So using different patterns you can generate parts for your desired password and then paste it like this:因此,使用不同的模式,您可以为所需的密码生成部分,然后像这样粘贴:

x <- stri_rand_strings(n=4, length=c(2,1,2,3), pattern=c("[a-z]","[A-Z]","[0-9]","[a-z]"))
x
## [1] "ex"  "N"   "81"  "tsy"
stri_flatten(x)
## [1] "exN81tsy"

Here's one approach这是一种方法

generate.password <- function(length,
                              capitals = integer(0),
                              numbers  = integer(0)) {

   stopifnot(is.numeric(length),   length   > 0L,
             is.numeric(capitals), capitals > 0L, capitals <= length,
             is.numeric(numbers),  numbers  > 0L, numbers  <= length,
             length(intersect(capitals, numbers)) == 0L)

   lc  <- sample(letters, length,           replace = TRUE)
   uc  <- sample(LETTERS, length(capitals), replace = TRUE)
   num <- sample(0:9,     length(numbers),  replace = TRUE)

   pass <- lc
   pass[capitals] <- uc
   pass[numbers]  <- num

   paste0(pass, collapse = "")
}


## Examples
set.seed(1)
generate.password(8)
# [1] "gjoxfxyr"

set.seed(1)
generate.password(length=8, capitals=c(2,4))
# [1] "gQoBfxyr"

set.seed(1)
generate.password(length=8, capitals=c(2,4), numbers=c(7:8))
# [1] "gQoBfx21"

You can also add other special characters in the same fashion.您还可以以相同的方式添加其他特殊字符。 If you want repeated values for letters and numbers, then add replace =TRUE in sample function.如果您想要字母和数字的重复值,则在sample函数中添加replace =TRUE

I liked the solution given by @Hadd E. Nuff... and What I did, is the inclusion of digits between 0 and 9, at random... here is the modified solution...我喜欢@Hadd E. Nuff 给出的解决方案......我所做的是随机包含0到9之间的数字......这是修改后的解决方案......

generate.password <- function(LENGTH){
punct <- c("!",  "#", "$", "%", "&", "(", ")", "*",  "+", "-", "/", ":", 
         ";", "<", "=", ">", "?", "@", "[", "^", "_", "{", "|", "}", "~")
nums <- c(0:9)
chars <- c(letters, LETTERS, punct, nums)
p <- c(rep(0.0105, 52), rep(0.0102, 25), rep(0.02, 10))
pword <- paste0(sample(chars, LENGTH, TRUE, prob = p), collapse = "")
return(pword)
}

generate.password(8)

This will generate very strong passwords like:这将生成非常强的密码,例如:

"C2~mD20U"         # 8 alpha-numeric-specialchar

"+J5Gi3"           # 6 alpha-numeric-specialchar

"77{h6RsGQJ66if5"  # 15 alpha-numeric-specialchar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM