2020-05-04 16:25:57 +08:00
|
|
|
// Copyright 2020 The Gogs Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package cryptoutil
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/sha1"
|
2022-06-05 13:34:21 +08:00
|
|
|
"crypto/sha256"
|
2020-05-04 16:25:57 +08:00
|
|
|
"encoding/hex"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// SHA1 encodes string to hexadecimal of SHA1 checksum.
|
|
|
|
|
func SHA1(str string) string {
|
|
|
|
|
h := sha1.New()
|
|
|
|
|
_, _ = h.Write([]byte(str))
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
|
}
|
2022-06-05 13:34:21 +08:00
|
|
|
|
|
|
|
|
// SHA256 encodes string to hexadecimal of SHA256 checksum.
|
|
|
|
|
func SHA256(str string) string {
|
|
|
|
|
h := sha256.New()
|
|
|
|
|
_, _ = h.Write([]byte(str))
|
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
|
}
|