• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang hmac.Sum函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中crypto/hmac.Sum函数的典型用法代码示例。如果您正苦于以下问题:Golang Sum函数的具体用法?Golang Sum怎么用?Golang Sum使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Sum函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: GetHMAC

func GetHMAC(hashType int, input, key []byte) []byte {
	var hash func() hash.Hash

	switch hashType {
	case HASH_SHA1:
		{
			hash = sha1.New
		}
	case HASH_SHA256:
		{
			hash = sha256.New
		}
	case HASH_SHA512:
		{
			hash = sha512.New
		}
	case HASH_SHA512_384:
		{
			hash = sha512.New384
		}
	}

	hmac := hmac.New(hash, []byte(key))
	hmac.Write(input)
	return hmac.Sum(nil)
}
开发者ID:jmaurice,项目名称:gocryptotrader,代码行数:26,代码来源:common.go


示例2: NewMasterKey

// Creates a new master extended key from a seed
func NewMasterKey(seed []byte) (*Key, error) {
	// Generate key and chaincode
	hmac := hmac.New(sha512.New, []byte("Bitcoin seed"))
	hmac.Write([]byte(seed))
	intermediary := hmac.Sum(nil)

	// Split it into our key and chain code
	keyBytes := intermediary[:32]
	chainCode := intermediary[32:]

	// Validate key
	err := validatePrivateKey(keyBytes)
	if err != nil {
		return nil, err
	}

	// Create the key struct
	key := &Key{
		Version:     PrivateWalletVersion,
		ChainCode:   chainCode,
		Key:         keyBytes,
		Depth:       0x0,
		ChildNumber: []byte{0x00, 0x00, 0x00, 0x00},
		FingerPrint: []byte{0x00, 0x00, 0x00, 0x00},
		IsPrivate:   true,
	}

	return key, nil
}
开发者ID:cpacia,项目名称:go-bip32,代码行数:30,代码来源:bip32.go


示例3: NewChildKey

// Derives a child key from a given parent as outlined by bip32
func (key *Key) NewChildKey(childIdx uint32) (*Key, error) {
	hardenedChild := childIdx >= FirstHardenedChild
	childIndexBytes := uint32Bytes(childIdx)

	// Fail early if trying to create hardned child from public key
	if !key.IsPrivate && hardenedChild {
		return nil, errors.New("Can't create hardened child for public key")
	}

	// Get intermediary to create key and chaincode from
	// Hardened children are based on the private key
	// NonHardened children are based on the public key
	var data []byte
	if hardenedChild {
		data = append([]byte{0x0}, key.Key...)
	} else {
		data = publicKeyForPrivateKey(key.Key)
	}
	data = append(data, childIndexBytes...)

	hmac := hmac.New(sha512.New, key.ChainCode)
	hmac.Write(data)
	intermediary := hmac.Sum(nil)

	// Create child Key with data common to all both scenarios
	childKey := &Key{
		ChildNumber: childIndexBytes,
		ChainCode:   intermediary[32:],
		Depth:       key.Depth + 1,
		IsPrivate:   key.IsPrivate,
	}

	// Bip32 CKDpriv
	if key.IsPrivate {
		childKey.Version = PrivateWalletVersion
		childKey.FingerPrint = hash160(publicKeyForPrivateKey(key.Key))[:4]
		childKey.Key = addPrivateKeys(intermediary[:32], key.Key)

		// Validate key
		err := validatePrivateKey(childKey.Key)
		if err != nil {
			return nil, err
		}
		// Bip32 CKDpub
	} else {
		keyBytes := publicKeyForPrivateKey(intermediary[:32])

		// Validate key
		err := validateChildPublicKey(keyBytes)
		if err != nil {
			return nil, err
		}

		childKey.Version = PublicWalletVersion
		childKey.FingerPrint = hash160(key.Key)[:4]
		childKey.Key = addPublicKeys(keyBytes, key.Key)
	}

	return childKey, nil
}
开发者ID:cpacia,项目名称:go-bip32,代码行数:61,代码来源:bip32.go


示例4: GetIronValue

func GetIronValue(name, value string, key []byte, timestamped bool) (val string, ok bool) {
	split := strings.SplitN(value, ":", 2)
	if len(split) != 2 {
		return
	}
	expected, value := []byte(split[0]), split[1]
	message := fmt.Sprintf("%s|%s", strings.Replace(name, "|", `\|`, -1), value)
	hmac := ironHMAC(key)
	hmac.Write([]byte(message))
	digest := hmac.Sum()
	mac := make([]byte, base64.URLEncoding.EncodedLen(len(digest)))
	base64.URLEncoding.Encode(mac, digest)
	if subtle.ConstantTimeCompare(mac, expected) != 1 {
		return
	}
	if timestamped {
		split = strings.SplitN(value, ":", 2)
		if len(split) != 2 {
			return
		}
		timestring, val := split[0], split[1]
		timestamp, err := strconv.Atoi64(timestring)
		if err != nil {
			return
		}
		if time.Seconds() > timestamp {
			return
		}
		return val, true
	}
	return value, true
}
开发者ID:gitreview,项目名称:ampify,代码行数:32,代码来源:crypto.go


示例5: hkdfExpand

// hkdfExpand implements HKDF-Expand from RFC 5869.
func hkdfExpand(hash func() hash.Hash, prk, info []byte, length int) []byte {
	hashSize := hash().Size()
	if length > 255*hashSize {
		panic("hkdfExpand: length too long")
	}
	if len(prk) < hashSize {
		panic("hkdfExpand: prk too short")
	}
	var lastBlock []byte
	counter := byte(0)
	okm := make([]byte, length)
	hmac := hmac.New(hash, prk)
	for length > 0 {
		hmac.Reset()
		counter++
		hmac.Write(lastBlock)
		hmac.Write(info)
		hmac.Write([]byte{counter})
		block := hmac.Sum(nil)
		lastBlock = block
		copy(okm[(int(counter)-1)*hashSize:], block)
		length -= hashSize
	}
	return okm
}
开发者ID:onedata,项目名称:helpers,代码行数:26,代码来源:hkdf.go


示例6: generateOAuthSignature

// Generates an OAuth signature using signatureBase
// and secret keys
func (t *Twitter) generateOAuthSignature(signatureBase string) string {
	signingKey := fmt.Sprintf("%s&%s", t.ConsumerSecret, t.OAuthTokenSecret)
	hmac := hmac.New(sha1.New, []byte(signingKey))

	hmac.Write([]byte(signatureBase))
	return base64.StdEncoding.EncodeToString(hmac.Sum(nil))
}
开发者ID:bsdf,项目名称:twitter,代码行数:9,代码来源:oauth.go


示例7: Signature

// Returns the signature to be used in the query string or Authorization header
func Signature(secret, toSign string) string {
	// Signature = Base64( HMAC-SHA1( UTF-8-Encoding-Of( YourSecretAccessKeyID, StringToSign ) ) );
	// Need to confirm what encoding go strings are when converted to []byte
	hmac := hmac.NewSHA1([]byte(secret))
	hmac.Write([]byte(toSign))

	return base64.StdEncoding.EncodeToString(hmac.Sum())
}
开发者ID:streadway,项目名称:s3sig,代码行数:9,代码来源:sign.go


示例8: signHMAC

func signHMAC(key []byte, data string) ([]byte, error) {
	hmac := hmac.New(sha256.New, []byte(key))
	_, err := hmac.Write([]byte(data))
	if err != nil {
		return nil, err
	}
	return hmac.Sum(nil), nil
}
开发者ID:p-lewis,项目名称:awsgolang,代码行数:8,代码来源:sign4.go


示例9: hkdfExtract

// hkdfExtract implements HKDF-Extract from RFC 5869.
func hkdfExtract(hash func() hash.Hash, salt, ikm []byte) []byte {
	if salt == nil {
		salt = make([]byte, hash().Size())
	}
	hmac := hmac.New(hash, salt)
	hmac.Write(ikm)
	return hmac.Sum(nil)
}
开发者ID:onedata,项目名称:helpers,代码行数:9,代码来源:hkdf.go


示例10: MakePermSignature

// MakePermSignature returns a string representing the signed permission
// hint for the blob identified by blob_hash, api_token and expiration timestamp.
func MakePermSignature(blob_hash string, api_token string, expiry string) string {
	hmac := hmac.New(sha1.New, PermissionSecret)
	hmac.Write([]byte(blob_hash))
	hmac.Write([]byte("@"))
	hmac.Write([]byte(api_token))
	hmac.Write([]byte("@"))
	hmac.Write([]byte(expiry))
	digest := hmac.Sum(nil)
	return fmt.Sprintf("%x", digest)
}
开发者ID:ntijanic,项目名称:arvados,代码行数:12,代码来源:perms.go


示例11: calcMac

// calcMac calculates HMAC-SHA-256 over the message using the passed secret key as
// input to the HMAC.
func calcMac(key [keyLen]byte, msg []byte) [securityParameter]byte {
	hmac := hmac.New(sha256.New, key[:])
	hmac.Write(msg)
	h := hmac.Sum(nil)

	var mac [securityParameter]byte
	copy(mac[:], h[:securityParameter])

	return mac
}
开发者ID:lightningnetwork,项目名称:lightning-onion,代码行数:12,代码来源:sphinx.go


示例12: VerifySignature

func (t *Token) VerifySignature(key string) error {
	sig_base := "rter_consumer=" + t.consumer + "&rter_resource=" + t.Resource + "&rter_valid_until=" + t.Valid_until
	hmac := hmac.New(sha256.New, bytes.NewBufferString(key).Bytes())
	checksig := url.QueryEscape(base64.StdEncoding.EncodeToString(hmac.Sum(bytes.NewBufferString(url.QueryEscape(sig_base)).Bytes())))

	if t.Signature == checksig {
		return nil
	}
	return errors.New("signature verification failed")
}
开发者ID:falahhaprak,项目名称:rter,代码行数:10,代码来源:token.go


示例13: makePermSignature

// makePermSignature generates a SHA-1 HMAC digest for the given blob,
// token, expiry, and site secret.
func makePermSignature(blobHash, apiToken, expiry string, permissionSecret []byte) string {
	hmac := hmac.New(sha1.New, permissionSecret)
	hmac.Write([]byte(blobHash))
	hmac.Write([]byte("@"))
	hmac.Write([]byte(apiToken))
	hmac.Write([]byte("@"))
	hmac.Write([]byte(expiry))
	digest := hmac.Sum(nil)
	return fmt.Sprintf("%x", digest)
}
开发者ID:Kunde21,项目名称:arvados,代码行数:12,代码来源:perms.go


示例14: IronString

func IronString(name, value string, key []byte, duration int64) string {
	if duration > 0 {
		value = fmt.Sprintf("%d:%s", time.Seconds()+duration, value)
	}
	message := fmt.Sprintf("%s|%s", strings.Replace(name, "|", `\|`, -1), value)
	hmac := ironHMAC(key)
	hmac.Write([]byte(message))
	mac := base64.URLEncoding.EncodeToString(hmac.Sum())
	return fmt.Sprintf("%s:%s", mac, value)
}
开发者ID:gitreview,项目名称:ampify,代码行数:10,代码来源:crypto.go


示例15: mkmac

func (sk *SKEME) mkmac(masterkey, Xb1, Xb2 []byte) (cipher.Stream, []byte) {
	keylen := sk.ms.KeySize()
	hmac := hmac.New(sk.suite.Hash, masterkey)
	hmac.Write(Xb1)
	hmac.Write(Xb2)
	key := hmac.Sum(nil)[:keylen]

	stream := sk.suite.Cipher(key)
	mac := random.Bytes(keylen, stream)
	return stream, mac
}
开发者ID:Liamsi,项目名称:crypto,代码行数:11,代码来源:skeme.go


示例16: Hash

// Create hash_ref suitable for later invocation of Check()
func (s ScryptAuth) Hash(pw_cost uint, user_password, salt []byte) (hash_ref []byte, err error) {
	scrypt_hash, err := scrypt.Key(user_password, salt, 1<<pw_cost, s.R, s.P, KEYLENGTH)
	if err != nil {
		return
	}
	hmac := hmac.New(sha256.New, s.HmacKey)
	if _, err = hmac.Write(scrypt_hash); err != nil {
		return
	}
	hash_ref = hmac.Sum(nil)
	return
}
开发者ID:spreadspace,项目名称:scryptauth,代码行数:13,代码来源:scryptauth.go


示例17: WithHMAC

// WithHMAC derives key of length outlen from the provided password, salt,
// and the number of iterations using PKCS#5 PBKDF2 with the provided
// hash function in HMAC.
//
// Caller is responsible to make sure that outlen < (2^32-1) * hash.Size().
func WithHMAC(hash func() hash.Hash, password []byte, salt []byte, iterations int, outlen int) []byte {
	out := make([]byte, outlen)
	hashSize := hash().Size()
	ibuf := make([]byte, 4)
	block := 1
	p := out
	for outlen > 0 {
		clen := outlen
		if clen > hashSize {
			clen = hashSize
		}

		ibuf[0] = byte((block >> 24) & 0xff)
		ibuf[1] = byte((block >> 16) & 0xff)
		ibuf[2] = byte((block >> 8) & 0xff)
		ibuf[3] = byte((block) & 0xff)

		hmac := hmac.New(hash, password)
		hmac.Write(salt)
		hmac.Write(ibuf)
		tmp := hmac.Sum()
		for i := 0; i < clen; i++ {
			p[i] = tmp[i]
		}

		for j := 1; j < iterations; j++ {
			hmac.Reset()
			hmac.Write(tmp)
			tmp = hmac.Sum()
			for k := 0; k < clen; k++ {
				p[k] ^= tmp[k]
			}
		}
		outlen -= clen
		block++
		p = p[clen:]
	}
	return out
}
开发者ID:cmc333333,项目名称:fragspace,代码行数:44,代码来源:pbkdf2.go


示例18: computeAuthTag

// Compute an authentication tag
func (ctx *cbcAEAD) computeAuthTag(aad, nonce, ciphertext []byte) []byte {
	buffer := make([]byte, len(aad)+len(nonce)+len(ciphertext)+8)
	n := 0
	n += copy(buffer, aad)
	n += copy(buffer[n:], nonce)
	n += copy(buffer[n:], ciphertext)
	binary.BigEndian.PutUint64(buffer[n:], uint64(len(aad)*8))

	// According to documentation, Write() on hash.Hash never fails.
	hmac := hmac.New(ctx.hash, ctx.integrityKey)
	_, _ = hmac.Write(buffer)

	return hmac.Sum(nil)[:ctx.authtagBytes]
}
开发者ID:CowLeo,项目名称:distribution,代码行数:15,代码来源:cbc_hmac.go


示例19: calcRootHash

func (mp *MerkleProof) calcRootHash(seed []byte, lastNode []byte) []byte {
	hmac := hmac.New(sha256.New, seed)
	for i := len(mp.Level) - 1; i >= 0; i-- {
		hmac.Reset()
		if mp.Level[i].NodeHash != nil {
			if mp.Level[i].IsLeft {
				hmac.Write(mp.Level[i].NodeHash)
				hmac.Write(lastNode)
			} else {
				hmac.Write(lastNode)
				hmac.Write(mp.Level[i].NodeHash)
			}
			lastNode = hmac.Sum(nil)
		}
	}
	return lastNode
}
开发者ID:utamaro,项目名称:go-heartbeat,代码行数:17,代码来源:merklebeat.go


示例20: TestTokenSigning

// test signing function independent of others
func TestTokenSigning(t *testing.T) {

	tok := FakeToken(TEST_TOKEN_URI, TEST_TOKEN_CONSUMER, TEST_TOKEN_LIFETIME_STR, "")
	if err := tok.Sign(TEST_TOKEN_SECRET); err != nil {
		t.Fatal("Sign() failed")
	}

	// verify signature manually
	var sig_base string
	sig_base = "rter_consumer=" + TEST_TOKEN_CONSUMER + "&rter_resource=" + TEST_TOKEN_URI + "&rter_valid_until=" + TEST_TOKEN_LIFETIME_STR
	hmac := hmac.New(sha256.New, bytes.NewBufferString(TEST_TOKEN_SECRET).Bytes())
	sig := url.QueryEscape(base64.StdEncoding.EncodeToString(hmac.Sum(bytes.NewBufferString(url.QueryEscape(sig_base)).Bytes())))

	if sig != tok.Signature {
		t.Error("signature mismatch")
	}
}
开发者ID:falahhaprak,项目名称:rter,代码行数:18,代码来源:token_test.go



注:本文中的crypto/hmac.Sum函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang hmac.Write函数代码示例发布时间:2022-05-24
下一篇:
Golang hmac.NewSHA1函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap