mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
33 lines
686 B
Go
33 lines
686 B
Go
// hash_sha1.go
|
|
package main
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
)
|
|
|
|
func main() {
|
|
hasher := sha1.New()
|
|
io.WriteString(hasher, "test")
|
|
b := []byte{}
|
|
fmt.Printf("Result: %x\n", hasher.Sum(b))
|
|
fmt.Printf("Result: %d\n", hasher.Sum(b))
|
|
//
|
|
hasher.Reset()
|
|
data := []byte("We shall overcome!")
|
|
n, err := hasher.Write(data)
|
|
if n != len(data) || err != nil {
|
|
log.Printf("Hash write error: %v / %v", n, err)
|
|
}
|
|
checksum := hasher.Sum(b)
|
|
fmt.Printf("Result: %x\n", checksum)
|
|
}
|
|
|
|
/* Output:
|
|
Result: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
|
|
Result: [169 74 143 229 204 177 155 166 28 76 8 115 211 145 233 135 152 47 187 211]
|
|
Result: e2222bfc59850bbb00a722e764a555603bb59b2a
|
|
*/
|