package main
import (
"fmt"
"golang.org/x/crypto/sha3"
)
func main() {
buf := []byte("some")
h := make([]byte, 32)
d := sha3.NewShake256()
d.Write(buf)
d.Read(h)
fmt.Printf("%x\n", h)
}
package main
import (
"fmt"
"golang.org/x/crypto/sha3"
)
func main() {
buf := []byte("some")
// A hash needs to be 64 bytes long to have 256-bit collision resistance.
h := make([]byte, 32)
// Compute a 64-byte hash of buf and put it in h.
sha3.ShakeSum256(h, buf)
fmt.Printf("%x\n", h)
}
package main
import (
"golang.org/x/crypto/ripemd160"
"fmt"
)
func main() {
h := ripemd160.New()
h.Write([]byte("hello"))
fmt.Printf("%x\n", h.Sum(nil))
}
package main
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
)
func main() {
key := []byte("I am key")
mac := hmac.New(sha256.New, key)
mac.Write([]byte("msg"))
fmt.Printf("%x\n", mac.Sum(nil))
}
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/md5"
"crypto/rand"
"fmt"
"hash"
"io"
"math/big"
"os"
)
func main() {
Curve := elliptic.P256() //see http://golang.org/pkg/crypto/elliptic/#P256
privatekey, err := ecdsa.GenerateKey(Curve, rand.Reader) // this generates a public & private key pair
if err != nil {
fmt.Println(err)
os.Exit(1)
}
pubkey := privatekey.PublicKey
fmt.Printf("%+v\n", privatekey.PublicKey.X)
fmt.Println("Private Key :")
fmt.Printf("%x \n", privatekey)
fmt.Println("Public Key :")
fmt.Printf("%x \n", pubkey)
fmt.Printf("%+v\n", pubkey) // 印出struct
// Sign ecdsa style
var h hash.Hash
h = md5.New()
r := big.NewInt(0)
s := big.NewInt(0)
io.WriteString(h, "This is a message to be signed and verified by ECDSA!")
signhash := h.Sum(nil)
r, s, serr := ecdsa.Sign(rand.Reader, privatekey, signhash)
if serr != nil {
fmt.Println(err)
os.Exit(1)
}
// Verify
verifystatus := ecdsa.Verify(&pubkey, signhash, r, s)
fmt.Println(verifystatus) // should be true
}