本文整理汇总了Golang中crypto/hmac.Write函数的典型用法代码示例。如果您正苦于以下问题:Golang Write函数的具体用法?Golang Write怎么用?Golang Write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Write函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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)
}
示例3: 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)
}
示例4: 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
}
示例5: 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
}
示例6: 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)
}
示例7: 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))
}
示例8: 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
}
示例9: 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
}
示例10: 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
}
示例11: 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())
}
示例12: 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)
}
示例13: 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
}
示例14: sign
func (bucket *Bucket) sign(req *http.Request) {
accessKeyId := bucket.Key
secretAccessKey := bucket.Secret
hmac := hmac.NewSHA1([]byte(secretAccessKey))
// method
msg := req.Method + "\n"
// md5sum
if md5, present := req.Header["Content-MD5"]; present {
msg += md5
}
msg += "\n"
// content-type
if contentType, present := req.Header["Content-Type"]; present {
msg += contentType
}
msg += "\n"
// date
msg += req.Header["Date"] + "\n"
// write out first four
hmac.Write([]byte(msg))
// add headers
rawpath := req.URL.Path
path := strings.TrimLeft(rawpath, "/")
// resource
resource := "/" + bucket.MainBucket + "/" + path
hmac.Write([]byte(resource))
// get a base64 encoding of the signature
encoded := new(bytes.Buffer)
encoder := base64.NewEncoder(base64.StdEncoding, encoded)
encoder.Write(hmac.Sum())
encoder.Close()
signature := encoded.String()
req.Header["Authorization"] = "AWS " + accessKeyId + ":" + signature
}
示例15: 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)
}