本文整理匯總了Golang中github.com/hyperledger/fabric/core/crypto/primitives.Hash函數的典型用法代碼示例。如果您正苦於以下問題:Golang Hash函數的具體用法?Golang Hash怎麽用?Golang Hash使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Hash函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: loadEnrollmentCertificate
func (node *nodeImpl) loadEnrollmentCertificate() error {
node.Debug("Loading enrollment certificate...")
cert, der, err := node.ks.loadCertX509AndDer(node.conf.getEnrollmentCertFilename())
if err != nil {
node.Errorf("Failed parsing enrollment certificate [%s].", err.Error())
return err
}
node.enrollCert = cert
// TODO: move this to retrieve
pk := node.enrollCert.PublicKey.(*ecdsa.PublicKey)
err = primitives.VerifySignCapability(node.enrollPrivKey, pk)
if err != nil {
node.Errorf("Failed checking enrollment certificate against enrollment key [%s].", err.Error())
return err
}
// Set node ID
node.id = primitives.Hash(der)
node.Debugf("Setting id to [% x].", node.id)
// Set eCertHash
node.enrollCertHash = primitives.Hash(der)
node.Debugf("Setting enrollCertHash to [% x].", node.enrollCertHash)
return nil
}
示例2: getTLSCertificateFromTLSCA
func (node *nodeImpl) getTLSCertificateFromTLSCA(id, affiliation string) (interface{}, []byte, error) {
node.Debug("getTLSCertificate...")
priv, err := primitives.NewECDSAKey()
if err != nil {
node.Errorf("Failed generating key: %s", err)
return nil, nil, err
}
uuid := util.GenerateUUID()
// Prepare the request
pubraw, _ := x509.MarshalPKIXPublicKey(&priv.PublicKey)
now := time.Now()
timestamp := timestamp.Timestamp{Seconds: int64(now.Second()), Nanos: int32(now.Nanosecond())}
req := &membersrvc.TLSCertCreateReq{
Ts: ×tamp,
Id: &membersrvc.Identity{Id: id + "-" + uuid},
Pub: &membersrvc.PublicKey{
Type: membersrvc.CryptoType_ECDSA,
Key: pubraw,
}, Sig: nil}
rawreq, _ := proto.Marshal(req)
r, s, err := ecdsa.Sign(rand.Reader, priv, primitives.Hash(rawreq))
if err != nil {
panic(err)
}
R, _ := r.MarshalText()
S, _ := s.MarshalText()
req.Sig = &membersrvc.Signature{Type: membersrvc.CryptoType_ECDSA, R: R, S: S}
pbCert, err := node.callTLSCACreateCertificate(context.Background(), req)
if err != nil {
node.Errorf("Failed requesting tls certificate: %s", err)
return nil, nil, err
}
node.Debug("Verifing tls certificate...")
tlsCert, err := primitives.DERToX509Certificate(pbCert.Cert.Cert)
certPK := tlsCert.PublicKey.(*ecdsa.PublicKey)
primitives.VerifySignCapability(priv, certPK)
node.Debug("Verifing tls certificate...done!")
return priv, pbCert.Cert.Cert, nil
}
示例3: init
func (handler *eCertTransactionHandlerImpl) init(client *clientImpl) error {
nonce, err := client.createTransactionNonce()
if err != nil {
client.error("Failed initiliazing transaction handler [%s]", err)
return err
}
handler.client = client
handler.nonce = nonce
handler.binding = primitives.Hash(append(handler.client.enrollCert.Raw, handler.nonce...))
return nil
}
示例4: init
func (handler *tCertTransactionHandlerImpl) init(tCertHandler *tCertHandlerImpl) error {
nonce, err := tCertHandler.client.createTransactionNonce()
if err != nil {
tCertHandler.client.error("Failed initiliazing transaction handler [%s]", err)
return err
}
handler.tCertHandler = tCertHandler
handler.nonce = nonce
handler.binding = primitives.Hash(append(handler.tCertHandler.tCert.GetCertificate().Raw, nonce...))
return nil
}
示例5: createConfidentialECertHDeployTransaction
func createConfidentialECertHDeployTransaction(t *testing.T) (*obc.Transaction, *obc.Transaction, error) {
uuid := util.GenerateUUID()
cds := &obc.ChaincodeDeploymentSpec{
ChaincodeSpec: &obc.ChaincodeSpec{
Type: obc.ChaincodeSpec_GOLANG,
ChaincodeID: &obc.ChaincodeID{Path: "Contract001"},
CtorMsg: nil,
ConfidentialityLevel: obc.ConfidentialityLevel_CONFIDENTIAL,
},
EffectiveDate: nil,
CodePackage: nil,
}
otx, err := obc.NewChaincodeDeployTransaction(cds, uuid)
if err != nil {
return nil, nil, err
}
handler, err := deployer.GetEnrollmentCertificateHandler()
if err != nil {
return nil, nil, err
}
txHandler, err := handler.GetTransactionHandler()
if err != nil {
return nil, nil, err
}
tx, err := txHandler.NewChaincodeDeployTransaction(cds, uuid)
// Check binding consistency
binding, _ := txHandler.GetBinding()
if !reflect.DeepEqual(binding, primitives.Hash(append(handler.GetCertificate(), tx.Nonce...))) {
t.Fatal("Binding is malformed!")
}
// Check confidentiality level
if tx.ConfidentialityLevel != cds.ChaincodeSpec.ConfidentialityLevel {
t.Fatal("Failed setting confidentiality level")
}
// Check metadata
if !reflect.DeepEqual(cds.ChaincodeSpec.Metadata, tx.Metadata) {
t.Fatal("Failed copying metadata")
}
return otx, tx, err
}
示例6: createConfidentialTCertHQueryTransaction
func createConfidentialTCertHQueryTransaction(t *testing.T) (*obc.Transaction, *obc.Transaction, error) {
uuid := util.GenerateUUID()
cis := &obc.ChaincodeInvocationSpec{
ChaincodeSpec: &obc.ChaincodeSpec{
Type: obc.ChaincodeSpec_GOLANG,
ChaincodeID: &obc.ChaincodeID{Path: "Contract001"},
CtorMsg: nil,
ConfidentialityLevel: obc.ConfidentialityLevel_CONFIDENTIAL,
},
}
otx, err := obc.NewChaincodeExecute(cis, uuid, obc.Transaction_CHAINCODE_QUERY)
if err != nil {
return nil, nil, err
}
handler, err := invoker.GetTCertificateHandlerNext()
if err != nil {
return nil, nil, err
}
txHandler, err := handler.GetTransactionHandler()
if err != nil {
return nil, nil, err
}
tx, err := txHandler.NewChaincodeQuery(cis, uuid)
// Check binding consistency
binding, _ := txHandler.GetBinding()
if !reflect.DeepEqual(binding, primitives.Hash(append(handler.GetCertificate(), tx.Nonce...))) {
t.Fatal("Binding is malformed!")
}
// Check confidentiality level
if tx.ConfidentialityLevel != cis.ChaincodeSpec.ConfidentialityLevel {
t.Fatal("Failed setting confidentiality level")
}
// Check metadata
if !reflect.DeepEqual(cis.ChaincodeSpec.Metadata, tx.Metadata) {
t.Fatal("Failed copying metadata")
}
return otx, tx, err
}
示例7: ECDSASign
// ECDSASign signs
func ECDSASign(signKey interface{}, msg []byte) ([]byte, error) {
temp := signKey.(*ecdsa.PrivateKey)
h := primitives.Hash(msg)
r, s, err := ecdsa.Sign(rand.Reader, temp, h)
if err != nil {
return nil, err
}
// R, _ := r.MarshalText()
// S, _ := s.MarshalText()
//
// fmt.Printf("r [%s], s [%s]\n", R, S)
raw, err := asn1.Marshal(ECDSASignature{r, s})
if err != nil {
return nil, err
}
return raw, nil
}
示例8: getEnrollmentCertificateFromECA
func (node *nodeImpl) getEnrollmentCertificateFromECA(id, pw string) (interface{}, []byte, []byte, error) {
// Get a new ECA Client
sock, ecaP, err := node.getECAClient()
defer sock.Close()
// Run the protocol
signPriv, err := primitives.NewECDSAKey()
if err != nil {
node.Errorf("Failed generating ECDSA key [%s].", err.Error())
return nil, nil, nil, err
}
signPub, err := x509.MarshalPKIXPublicKey(&signPriv.PublicKey)
if err != nil {
node.Errorf("Failed mashalling ECDSA key [%s].", err.Error())
return nil, nil, nil, err
}
encPriv, err := primitives.NewECDSAKey()
if err != nil {
node.Errorf("Failed generating Encryption key [%s].", err.Error())
return nil, nil, nil, err
}
encPub, err := x509.MarshalPKIXPublicKey(&encPriv.PublicKey)
if err != nil {
node.Errorf("Failed marshalling Encryption key [%s].", err.Error())
return nil, nil, nil, err
}
req := &membersrvc.ECertCreateReq{
Ts: ×tamp.Timestamp{Seconds: time.Now().Unix(), Nanos: 0},
Id: &membersrvc.Identity{Id: id},
Tok: &membersrvc.Token{Tok: []byte(pw)},
Sign: &membersrvc.PublicKey{Type: membersrvc.CryptoType_ECDSA, Key: signPub},
Enc: &membersrvc.PublicKey{Type: membersrvc.CryptoType_ECDSA, Key: encPub},
Sig: nil}
resp, err := ecaP.CreateCertificatePair(context.Background(), req)
if err != nil {
node.Errorf("Failed invoking CreateCertficatePair [%s].", err.Error())
return nil, nil, nil, err
}
if resp.FetchResult != nil && resp.FetchResult.Status != membersrvc.FetchAttrsResult_SUCCESS {
node.Warning(resp.FetchResult.Msg)
}
//out, err := rsa.DecryptPKCS1v15(rand.Reader, encPriv, resp.Tok.Tok)
spi := ecies.NewSPI()
eciesKey, err := spi.NewPrivateKey(nil, encPriv)
if err != nil {
node.Errorf("Failed parsing decrypting key [%s].", err.Error())
return nil, nil, nil, err
}
ecies, err := spi.NewAsymmetricCipherFromPublicKey(eciesKey)
if err != nil {
node.Errorf("Failed creating asymmetrinc cipher [%s].", err.Error())
return nil, nil, nil, err
}
out, err := ecies.Process(resp.Tok.Tok)
if err != nil {
node.Errorf("Failed decrypting toke [%s].", err.Error())
return nil, nil, nil, err
}
req.Tok.Tok = out
req.Sig = nil
hash := primitives.NewHash()
raw, _ := proto.Marshal(req)
hash.Write(raw)
r, s, err := ecdsa.Sign(rand.Reader, signPriv, hash.Sum(nil))
if err != nil {
node.Errorf("Failed signing [%s].", err.Error())
return nil, nil, nil, err
}
R, _ := r.MarshalText()
S, _ := s.MarshalText()
req.Sig = &membersrvc.Signature{Type: membersrvc.CryptoType_ECDSA, R: R, S: S}
resp, err = ecaP.CreateCertificatePair(context.Background(), req)
if err != nil {
node.Errorf("Failed invoking CreateCertificatePair [%s].", err.Error())
return nil, nil, nil, err
}
// Verify response
//.........這裏部分代碼省略.........
示例9: requestTLSCertificate
func requestTLSCertificate(t *testing.T) {
var opts []grpc.DialOption
creds, err := credentials.NewClientTLSFromFile(viper.GetString("server.tls.cert.file"), "tlsca")
if err != nil {
t.Logf("Failed creating credentials for TLS-CA client: %s", err)
t.Fail()
}
opts = append(opts, grpc.WithTransportCredentials(creds))
sockP, err := grpc.Dial(viper.GetString("peer.pki.tlsca.paddr"), opts...)
if err != nil {
t.Logf("Failed dialing in: %s", err)
t.Fail()
}
defer sockP.Close()
tlscaP := membersrvc.NewTLSCAPClient(sockP)
// Prepare the request
id := "peer"
priv, err := primitives.NewECDSAKey()
if err != nil {
t.Logf("Failed generating key: %s", err)
t.Fail()
}
uuid := util.GenerateUUID()
pubraw, _ := x509.MarshalPKIXPublicKey(&priv.PublicKey)
now := time.Now()
timestamp := timestamp.Timestamp{Seconds: int64(now.Second()), Nanos: int32(now.Nanosecond())}
req := &membersrvc.TLSCertCreateReq{
Ts: ×tamp,
Id: &membersrvc.Identity{Id: id + "-" + uuid},
Pub: &membersrvc.PublicKey{
Type: membersrvc.CryptoType_ECDSA,
Key: pubraw,
}, Sig: nil}
rawreq, _ := proto.Marshal(req)
r, s, err := ecdsa.Sign(rand.Reader, priv, primitives.Hash(rawreq))
if err != nil {
t.Logf("Failed signing the request: %s", err)
t.Fail()
}
R, _ := r.MarshalText()
S, _ := s.MarshalText()
req.Sig = &membersrvc.Signature{Type: membersrvc.CryptoType_ECDSA, R: R, S: S}
resp, err := tlscaP.CreateCertificate(context.Background(), req)
if err != nil {
t.Logf("Failed requesting tls certificate: %s", err)
t.Fail()
}
storePrivateKeyInClear("tls_peer.priv", priv, t)
storeCert("tls_peer.cert", resp.Cert.Cert, t)
storeCert("tls_peer.ca", resp.RootCert.Cert, t)
}
示例10: getStateEncryptor1_2
func (validator *validatorImpl) getStateEncryptor1_2(deployTx, executeTx *obc.Transaction) (StateEncryptor, error) {
// Check nonce
if deployTx.Nonce == nil || len(deployTx.Nonce) == 0 {
return nil, errors.New("Invalid deploy nonce.")
}
if executeTx.Nonce == nil || len(executeTx.Nonce) == 0 {
return nil, errors.New("Invalid invoke nonce.")
}
// Check ChaincodeID
if deployTx.ChaincodeID == nil {
return nil, errors.New("Invalid deploy chaincodeID.")
}
if executeTx.ChaincodeID == nil {
return nil, errors.New("Invalid execute chaincodeID.")
}
// Check that deployTx and executeTx refers to the same chaincode
if !reflect.DeepEqual(deployTx.ChaincodeID, executeTx.ChaincodeID) {
return nil, utils.ErrDifferentChaincodeID
}
// Check the confidentiality protocol version
if deployTx.ConfidentialityProtocolVersion != executeTx.ConfidentialityProtocolVersion {
return nil, utils.ErrDifferrentConfidentialityProtocolVersion
}
validator.Debugf("Parsing transaction. Type [%s]. Confidentiality Protocol Version [%s]", executeTx.Type.String(), executeTx.ConfidentialityProtocolVersion)
deployStateKey, err := validator.getStateKeyFromTransaction(deployTx)
if executeTx.Type == obc.Transaction_CHAINCODE_QUERY {
validator.Debug("Parsing Query transaction...")
executeStateKey, err := validator.getStateKeyFromTransaction(executeTx)
// Compute deployTxKey key from the deploy transaction. This is used to decrypt the actual state
// of the chaincode
deployTxKey := primitives.HMAC(deployStateKey, deployTx.Nonce)
// Compute the key used to encrypt the result of the query
//queryKey := utils.HMACTruncated(executeStateKey, append([]byte{6}, executeTx.Nonce...), utils.AESKeyLength)
// Init the state encryptor
se := queryStateEncryptor{}
err = se.init(validator.nodeImpl, executeStateKey, deployTxKey)
if err != nil {
return nil, err
}
return &se, nil
}
// Compute deployTxKey key from the deploy transaction
deployTxKey := primitives.HMAC(deployStateKey, deployTx.Nonce)
// Mask executeTx.Nonce
executeTxNonce := primitives.HMACTruncated(deployTxKey, primitives.Hash(executeTx.Nonce), primitives.NonceSize)
// Compute stateKey to encrypt the states and nonceStateKey to generates IVs. This
// allows validators to reach consesus
stateKey := primitives.HMACTruncated(deployTxKey, append([]byte{3}, executeTxNonce...), primitives.AESKeyLength)
nonceStateKey := primitives.HMAC(deployTxKey, append([]byte{4}, executeTxNonce...))
// Init the state encryptor
se := stateEncryptorImpl{}
err = se.init(validator.nodeImpl, stateKey, nonceStateKey, deployTxKey, executeTxNonce)
if err != nil {
return nil, err
}
return &se, nil
}