Search code examples
rstringrandom

Function to generate a random password


I want to generate a random password for employees with the function below. This is my first attempt with functions in R. So I need a bit help.

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)                
          }

}

With the answer form Floo0 I've changed the function to somthing like that, but it doesn't work:

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="")
          }

}

Solution

  • What about

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

    result is something like this

    "HKF§VvnD"
    

    Caution: This approch does not enforce having capitals, numbers, and non alpha numeric symbols

    EDIT:

    If you want to enforce a certain number of capitals, numbers, and non alpha numeric symbols you could go with this:

    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="")
    

    Where c(2,2,2,2) specifies the number of numbers, letters, capital letters and symbild (in this order). Result:

    [1] "j$bP%5R3"
    

    EDIT2: To produce an new column in you table mitarbeiter just use

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