diff options
author | Antoine GIRARD <sapk@users.noreply.github.com> | 2019-06-16 09:50:46 +0200 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-06-16 08:50:46 +0100 |
commit | 367aeb169ac9f213a047bad24bbb3a4f58e3362a (patch) | |
tree | b8524b1f9c8caaccbeb5ccc436937cc1d3a70c41 /models/ssh_key.go | |
parent | cf2221e3acce4d2c88c09976557ce3e73068baaf (diff) | |
download | gitea-367aeb169ac9f213a047bad24bbb3a4f58e3362a.tar.gz gitea-367aeb169ac9f213a047bad24bbb3a4f58e3362a.zip |
Use go method to calculate ssh key fingerprint (#7128)
* Use go method to calculate key fingerprint
* add gitea copyright
* use native go method only for built-in server
* refactor and add tests
* add gitea copyright
Diffstat (limited to 'models/ssh_key.go')
-rw-r--r-- | models/ssh_key.go | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/models/ssh_key.go b/models/ssh_key.go index 1f2288b13e..15a10826d8 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -359,7 +360,7 @@ func checkKeyFingerprint(e Engine, fingerprint string) error { return nil } -func calcFingerprint(publicKeyContent string) (string, error) { +func calcFingerprintSSHKeygen(publicKeyContent string) (string, error) { // Calculate fingerprint. tmpPath, err := writeTmpKeyFile(publicKeyContent) if err != nil { @@ -375,6 +376,34 @@ func calcFingerprint(publicKeyContent string) (string, error) { return strings.Split(stdout, " ")[1], nil } +func calcFingerprintNative(publicKeyContent string) (string, error) { + // Calculate fingerprint. + pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKeyContent)) + if err != nil { + return "", err + } + return ssh.FingerprintSHA256(pk), nil +} + +func calcFingerprint(publicKeyContent string) (string, error) { + //Call the method based on configuration + var ( + fnName, fp string + err error + ) + if setting.SSH.StartBuiltinServer { + fnName = "calcFingerprintNative" + fp, err = calcFingerprintNative(publicKeyContent) + } else { + fnName = "calcFingerprintSSHKeygen" + fp, err = calcFingerprintSSHKeygen(publicKeyContent) + } + if err != nil { + return "", fmt.Errorf("%s: %v", fnName, err) + } + return fp, nil +} + func addKey(e Engine, key *PublicKey) (err error) { if len(key.Fingerprint) == 0 { key.Fingerprint, err = calcFingerprint(key.Content) |