如何避免golang的“密码短语太短”

What I am doing is to create a ssh key from a golang program, using

exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa", "-P", "\"\"")

but the output fails

exit status 1: Saving key "id_rsa" failed: passphrase is too short (minimum five characters)

If I execute the command in the terminal it works perfect.

Generating public/private rsa key pair.
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
SHA256:HYixKOtEB42jAr+XNa9uQMZnvmLHa6Sirq+ietWaxzI admin@MacBook-Pro.local
The key's randomart image is:
+---[RSA 2048]----+
|  .o  .          |
|. o... + .       |
|.oooo o . .      |
|o..++ =  . .     |
|. o+ B oS .      |
| o. = + .        |
|  .o O o         |
|. o E X          |
|%*.o Xo.         |
+----[SHA256]-----+

The complete code is:

package main

import (
    "bytes"
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa", "-P", "\"\"")
    var out bytes.Buffer
    var stderr bytes.Buffer
    cmd.Stdout = &out
    cmd.Stderr = &stderr
    err := cmd.Run()
    if err != nil {
        fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
        return
    }
    fmt.Println("Result: " + out.String())
}
ssh

1个回答



您无需指定空的-P属性,只需使用:</ p>

   exec.Command(“ ssh-keygen”,“-f”,“ id_rsa”,“-t”,“ rsa”)
</ code> </ pre>

任何需要指定空-P </ p>

  exec.Command(“ ssh-keygen”,“ -f”,“ id_rsa”,“ -t”,“ rsa”  ,“ -P”,“”)
</ code> </ pre>

在创建id_rsa文件之前,请确保该文件在您创建的目录中不存在,否则您将 得到退出错误1:</ p>
</ div>

展开原文

原文

You don't need to specify an empty -P attribute, just simply use:

exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa")

If for any reason you need to specify empty -P

exec.Command("ssh-keygen", "-f", "id_rsa", "-t", "rsa", "-P", "")

Also before creating the id_rsa file make sure it doesn't exist in the directory you are creating it, otherwise you will get exit error 1:

doukun1450
doukun1450 非常感谢@VaneTrajkov。 它完美的工作:)
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问