projects:windows:ssh_phrase
Различия
Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слеваПредыдущая версияСледующая версия | Предыдущая версия | ||
projects:windows:ssh_phrase [2025/04/15 18:32] – | projects:windows:ssh_phrase [2025/04/15 23:01] (текущий) – | ||
---|---|---|---|
Строка 6: | Строка 6: | ||
**Программа в архиве: | **Программа в архиве: | ||
- | {{ : | + | {{ : |
+ | |||
+ | |||
+ | <WRAP center round important 90%> | ||
+ | < | ||
+ | в ssh приватном ключе вида | ||
+ | -----BEGIN OPENSSH PRIVATE KEY----- | ||
+ | b3BlbnNzaC1rZXktZ...много base64 | ||
+ | -----END OPENSSH PRIVATE KEY----- | ||
+ | обязательно должен быть символ переноса на новую строку в конце файла, | ||
+ | иначе будет ошибка при попытке подключения | ||
+ | </ | ||
+ | </ | ||
+ | |||
**Исходный код:** | **Исходный код:** | ||
++++sshphrase.go| | ++++sshphrase.go| | ||
<code go sshphrase.go> | <code go sshphrase.go> | ||
- | // sshphrase.go | + | // sshphrase.go |
package main | package main | ||
Строка 27: | Строка 41: | ||
) | ) | ||
- | // ===== marshalOpenSSHPrivateKey: | + | type Pair struct { |
- | func marshalOpenSSHPrivateKey(privateKey | + | Private []byte |
- | // Структура для OpenSSH-ключа | + | Public |
- | type opensshPrivateKey struct | + | } |
- | Check1 uint32 | + | |
- | Check2 uint32 | + | func PairFromED25519(public ed25519.PublicKey, |
- | Key struct { | + | privBlock, err := ssh.MarshalPrivateKey(private, |
- | Curve | + | if err != nil { |
- | PubKey []byte | + | return nil, err |
- | Priv | + | |
- | } | + | |
} | } | ||
- | pub := privateKey.Public().(ed25519.PublicKey) | + | var buf bytes.Buffer |
- | buf := &bytes.Buffer{} | + | err = pem.Encode(&buf, privBlock) |
- | buf.Write([]byte(" | + | |
- | writeString(buf, " | + | |
- | writeString(buf, | + | |
- | writeString(buf, | + | |
- | writeUint32(buf, | + | |
- | + | ||
- | pubKey, err := ssh.NewPublicKey(pub) | + | |
if err != nil { | if err != nil { | ||
return nil, err | return nil, err | ||
} | } | ||
- | writeString(buf, | ||
- | content | + | pubKey, err := ssh.NewPublicKey(public) |
- | check := uint32(0x01020304) // контрольное значение для валидации | + | if err != nil { |
- | writeUint32(content, | + | return nil, err |
- | writeUint32(content, | + | |
- | writeString(content, | + | |
- | writeString(content, | + | |
- | writeString(content, | + | |
- | content.WriteByte(0) // завершающий ноль | + | |
- | + | ||
- | padLen := 8 - (content.Len() % 8) | + | |
- | for i := 1; i <= padLen; i++ { | + | |
- | content.WriteByte(byte(i)) | + | |
} | } | ||
- | writeString(buf, | ||
- | final := pem.EncodeToMemory(&pem.Block{ | + | return |
- | Type: " | + | Private: buf.Bytes(), |
- | Bytes: buf.Bytes(), | + | Public: ssh.MarshalAuthorizedKey(pubKey), |
- | }) | + | }, nil |
- | return final, nil | + | |
} | } | ||
- | // ===== Конец marshalOpenSSHPrivateKey ===== | ||
- | |||
- | func writeString(w *bytes.Buffer, | ||
- | writeUint32(w, | ||
- | w.WriteString(s) | ||
- | } | ||
- | |||
- | func writeUint32(w *bytes.Buffer, | ||
- | w.WriteByte(byte(v >> 24)) | ||
- | w.WriteByte(byte(v >> 16)) | ||
- | w.WriteByte(byte(v >> 8)) | ||
- | w.WriteByte(byte(v)) | ||
- | } | ||
- | |||
- | // ===== main: CLI-меню, | ||
func main() { | func main() { | ||
fmt.Print(" | fmt.Print(" | ||
Строка 100: | Строка 78: | ||
phrase := string(b) | phrase := string(b) | ||
- | // Создание seed на основе хэша введённой фразы | ||
seed := sha256.Sum256([]byte(phrase)) | seed := sha256.Sum256([]byte(phrase)) | ||
privateKey := ed25519.NewKeyFromSeed(seed[: | privateKey := ed25519.NewKeyFromSeed(seed[: | ||
- | publicKey, err := ssh.NewPublicKey(privateKey.Public()) | + | publicKey := privateKey.Public().(ed25519.PublicKey) |
+ | |||
+ | pair, err := PairFromED25519(publicKey, | ||
if err != nil { | if err != nil { | ||
- | log.Fatalf(" | + | log.Fatalf(" |
} | } | ||
Строка 121: | Строка 100: | ||
case " | case " | ||
fmt.Println(" | fmt.Println(" | ||
- | fmt.Println(string(ssh.MarshalAuthorizedKey(publicKey))) | + | fmt.Println(string(pair.Public)) |
case " | case " | ||
fmt.Println(" | fmt.Println(" | ||
- | keyBytes, err := marshalOpenSSHPrivateKey(privateKey) | + | fmt.Println(string(pair.Private)) |
- | if err != nil { | + | |
- | log.Fatalf(" | + | |
- | } | + | |
- | fmt.Println(string(keyBytes)) | + | |
fmt.Println(" | fmt.Println(" | ||
Строка 141: | Строка 116: | ||
} | } | ||
} | } | ||
- | |||
- | // ===== Конец main ===== | ||
- | |||
</ | </ |
projects/windows/ssh_phrase.1744731156.txt.gz · Последнее изменение: 2025/04/15 18:32 —