String R中的密码生成器函数

String R中的密码生成器函数,string,r,passwords,cryptography,String,R,Passwords,Cryptography,我正在寻找一种在R中编写密码生成器函数的智能方法: generate.password (length, capitals, numbers) 长度:密码的长度 大写:定义大写字母出现位置的向量,该向量反映对应的密码字符串位置,默认为无大写字母 数字:定义大写字母出现位置的向量,该向量反映对应的密码字符串位置,默认为无数字 示例: generate.password(8) [1] "hqbfpozr" generate.password(length=8, capitals=c(2,4

我正在寻找一种在R中编写密码生成器函数的智能方法:

generate.password (length, capitals, numbers)
  • 长度:密码的长度
  • 大写:定义大写字母出现位置的向量,该向量反映对应的密码字符串位置,默认为无大写字母
  • 数字:定义大写字母出现位置的向量,该向量反映对应的密码字符串位置,默认为无数字
示例:

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"
这里有一种方法

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"
generate.password 0L,

is.numeric(大写),capitals>0L,capitals 0L,numbers在(版本>=0.2-3)包中有一个生成随机字符串的函数:

因此,使用不同的模式,您可以生成所需密码的部件,然后按如下方式粘贴:

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"

x我喜欢@hadde.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)

我肯定这里有一个问题,但我的眼睛现在不行。检查软件包
摘要
,尤其是函数
hmac
。正确的马电池钉-为什么这是一个糟糕的密码方案:有一个名为password的软件包。是否可以让stringi只使用可很好打印的ASCII字符?像asc.read
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)
"C2~mD20U"         # 8 alpha-numeric-specialchar

"+J5Gi3"           # 6 alpha-numeric-specialchar

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