【问题标题】:Way to securely give a password to R application from the terminal?从终端安全地向 R 应用程序提供密码的方法?
【发布时间】:2011-02-24 10:08:58
【问题描述】:

R 是否具有允许用户安全地提供密码的功能,例如 Python 的 getpass 模块?

(请参阅http://docs.python.org/library/getpass.html 了解我的意思的示例)

【问题讨论】:

    标签: r input passwords password-protection


    【解决方案1】:

    问题是 R 没有功能来控制它正在运行的终端(类似于Rncurses);这可能是由于便携性问题。
    前段时间我在同样的问题上苦苦挣扎,最后我得到了一个使用 TclTk 的函数:

    getPass<-function(){  
      require(tcltk);  
      wnd<-tktoplevel();tclVar("")->passVar;  
      #Label  
      tkgrid(tklabel(wnd,text="Enter password:"));  
      #Password box  
      tkgrid(tkentry(wnd,textvariable=passVar,show="*")->passBox);  
      #Hitting return will also submit password  
      tkbind(passBox,"<Return>",function() tkdestroy(wnd));  
      #OK button  
      tkgrid(tkbutton(wnd,text="OK",command=function() tkdestroy(wnd)));  
      #Wait for user to click OK  
      tkwait.window(wnd);  
      password<-tclvalue(passVar);  
      return(password);  
    }  
    

    当然它不会在非 GUI 环境中工作。

    【讨论】:

    • 像宣传的那样完美运行。谢谢!
    • 刚刚发现这是在 getPass::getPass() 函数中实现的。
    【解决方案2】:

    终端安全密码问题的非常简单的linux概念:

       password <- function(prompt = "Password:"){
          cat(prompt)
          pass <- system('stty -echo && read ff && stty echo && echo $ff && ff=""',
                            intern=TRUE)
          cat('\n')
          invisible(pass)
       }        
    

    【讨论】:

    • 对我不起作用。薄荷 18.2。 &gt; password() Password:stty: 'standard input': Inappropriate ioctl for device Warning message: running command 'stty -echo &amp;&amp; read ff &amp;&amp; stty echo &amp;&amp; echo $ff &amp;&amp; ff=""' had status 1
    【解决方案3】:

    根据上述 cmets 中的 m-dz,现在有一个名为 getPass 的包用于执行此操作,它只有一个函数 getPass()。这是base::readline() 的替代品。

    【讨论】:

    • 它似乎也回退到从命令行读取。 +1。
    【解决方案4】:

    我的包 keyringr 通过从底层操作系统密钥环(Windows 上的 DPAPI、OSX 上的 Keychain 和 Linux 上的 Gnome 密钥环)检索密码来解决这个问题。

    vignette 详细说明了如何使用该软件包,但如果您使用的是 OSX 并且密码保存在 Keychain 中,您可以使用以下命令将密码返回给 R(其中 mydb_myuser 是钥匙串项目名称):

    install.packages("keyringr")
    library("keyringr")
    mypwd <- decrypt_kc_pw("mydb_myuser")
    print(mypwd)
    

    【讨论】:

    • 我收到Error: Sys.info()["sysname"] == "Darwin" is not TRUE。那是什么意思?我应该按Alt-Right吗?
    • 您使用的是什么操作系统?请在 github.com/jgilfillan/keyringr 记录问题。谢谢。
    • "Darwin" is not TRUE 在 Windows 上。
    【解决方案5】:

    这是一个基于?modalDialog的登录弹出窗口。

    library("shiny")
    
    shinyApp(
      ui <- basicPage(
        actionButton("login", "Login"),
        verbatimTextOutput("secrets")
      ),
    
      server <- function(input, output, session) {
        vals <- reactiveValues(authenticated=FALSE)
    
        passwordModal <- function(message=NULL) {
          modalDialog(
            textInput("username", "Username", input$username),
            passwordInput("password", "Password", input$password),
    
            if (!is.null(message)) div(tags$b(message, style="color: red;")),
    
            footer = tagList(
              modalButton("Cancel"),
              actionButton("authenticate", "OK")
            )
          )
        }
    
        observeEvent(input$login, {
          showModal(passwordModal())
        })
    
        observeEvent(input$authenticate, {
          vals$authenticated <- FALSE
          if (!is.null(input$username) && nzchar(input$username) &&
              !is.null(input$password) && nzchar(input$password)) {
            removeModal()
    
            if (input$password == "letmein") {
              vals$authenticated <- TRUE
            } else {
              showModal(passwordModal(message="Incorrect password!"))
            }
    
          } else {
            showModal(passwordModal(message="Please fill in your username and password"))
          }
        })
    
        output$secrets <- renderText({
          if (vals$authenticated) {
            paste("Don't tell anyone, ", input$username, ", but...", sep="")
          } else {
            "I can't tell you that!"
          }
        })
      }
    )
    

    【讨论】:

      猜你喜欢
      • 2013-06-13
      • 2011-01-22
      • 2011-07-19
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多