當前位置: 首頁>>代碼示例>>Golang>>正文


Golang hmac.NewSHA1函數代碼示例

本文整理匯總了Golang中crypto/hmac.NewSHA1函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewSHA1函數的具體用法?Golang NewSHA1怎麽用?Golang NewSHA1使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewSHA1函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: AuthenticateServer

func AuthenticateServer(conn net.Conn, secret []byte) os.Error {
	challenge := make([]byte, challengeLength)
	n, err := conn.Read(challenge)
	if err != nil {
		return err
	}
	challenge = challenge[:n]

	h := hmac.NewSHA1(secret)
	h.Write(challenge)
	_, err = conn.Write(h.Sum())
	if err != nil {
		return err
	}

	expectAck := []byte("OK")
	ack := make([]byte, len(expectAck))
	n, err = conn.Read(ack)
	if err != nil {
		return err
	}

	ack = ack[:n]
	if bytes.Compare(expectAck, ack) != 0 {
		fmt.Println(expectAck, ack)
		return os.NewError("Missing ack reply")
	}
	return nil
}
開發者ID:krasin,項目名稱:termite,代碼行數:29,代碼來源:connection.go

示例2: HashString

func HashString(key []byte, hashThis string) string {
	// log.Println( "Hashing this string: ", hashThis )
	// log.Println( "SHA1 Key: ", key )
	sha1 := hmac.NewSHA1(key)
	sha1.Write([]byte(hashThis))
	return base64.StdEncoding.EncodeToString(sha1.Sum())
}
開發者ID:natelong,項目名稱:playlistr,代碼行數:7,代碼來源:easyauth.go

示例3: GetSignature

// Calculates the HMAC-SHA1 signature of a base string, given a consumer and
// token secret.
func (s *HmacSha1Signer) GetSignature(consumerSecret string, tokenSecret string, signatureBase string) string {
	signingKey := consumerSecret + "&" + tokenSecret
	signer := hmac.NewSHA1([]byte(signingKey))
	signer.Write([]byte(signatureBase))
	oauthSignature := base64.StdEncoding.EncodeToString(signer.Sum())
	return oauthSignature
}
開發者ID:paul-lalonde,項目名稱:golibs,代碼行數:9,代碼來源:oauth1a.go

示例4: genSig

func (y *Yubikey) genSig(params map[string]string) string {
	keys := make([]string, len(params))
	i := 0
	for k, _ := range params {
		keys[i] = k
		i++
	}
	sort.Strings(keys)

	buf := bytes.NewBuffer([]byte{})

	for i, key := range keys {
		buf.WriteString(key + "=" + params[key])
		if i != len(keys)-1 {
			buf.WriteString("&")
		}
	}

	h := hmac.NewSHA1(y.key)

	h.Write(buf.Bytes())

	rawSig := h.Sum()

	sig := make([]byte, base64.StdEncoding.EncodedLen(len(rawSig)))
	base64.StdEncoding.Encode(sig, rawSig)

	return string(sig)
}
開發者ID:jeffreybolle,項目名稱:goyubikey,代碼行數:29,代碼來源:yubikey.go

示例5: setupKeys

// setupKeys sets the cipher and MAC keys from kex.K, kex.H and sessionId, as
// described in RFC 4253, section 6.4. direction should either be serverKeys
// (to setup server->client keys) or clientKeys (for client->server keys).
func (c *common) setupKeys(d direction, K, H, sessionId []byte, hashFunc crypto.Hash) error {
	cipherMode := cipherModes[c.cipherAlgo]

	macKeySize := 20

	iv := make([]byte, cipherMode.ivSize)
	key := make([]byte, cipherMode.keySize)
	macKey := make([]byte, macKeySize)

	h := hashFunc.New()
	generateKeyMaterial(iv, d.ivTag, K, H, sessionId, h)
	generateKeyMaterial(key, d.keyTag, K, H, sessionId, h)
	generateKeyMaterial(macKey, d.macKeyTag, K, H, sessionId, h)

	c.mac = truncatingMAC{12, hmac.NewSHA1(macKey)}

	cipher, err := cipherMode.createCipher(key, iv)
	if err != nil {
		return err
	}

	c.cipher = cipher

	return nil
}
開發者ID:davecheney,項目名稱:ssh,代碼行數:28,代碼來源:transport.go

示例6: AuthenticateClient

func AuthenticateClient(conn net.Conn, secret []byte) os.Error {
	challenge := make([]byte, 0)
	for i := 0; i < challengeLength; i++ {
		challenge = append(challenge, byte(rand.Int31n(256)))
	}

	_, err := conn.Write(challenge)
	if err != nil {
		return err
	}

	h := hmac.NewSHA1(secret)
	_, err = h.Write(challenge)
	expected := h.Sum()
	response := make([]byte, len(expected))
	n, err := conn.Read(response)
	if err != nil {
		return err
	}
	response = response[:n]

	if bytes.Compare(response, expected) != 0 {
		log.Println("Authentication failure from", conn.RemoteAddr())
		return os.NewError("Mismatch in response")
	}
	conn.Write([]byte("OK"))
	return nil
}
開發者ID:krasin,項目名稱:termite,代碼行數:28,代碼來源:connection.go

示例7: getCookieSig

func getCookieSig(key string, val []byte, timestamp string) string {
	hm := hmac.NewSHA1([]byte(key))

	hm.Write(val)

	hex := fmt.Sprintf("%02x", hm.Sum([]byte(timestamp)))
	return hex
}
開發者ID:TheOnly92,項目名稱:web.go,代碼行數:8,代碼來源:web.go

示例8: getCookieSig

func getCookieSig(val []byte, timestamp string) string {
	hm := hmac.NewSHA1([]byte(serverSecret))
	hm.Write(val)
	hm.Write([]byte(timestamp))

	hex := fmt.Sprintf("%02x", hm.Sum())
	return hex
}
開發者ID:AaronO,項目名稱:lightwave,代碼行數:8,代碼來源:server.go

示例9: 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

示例10: sign

// ----------------------------------------------------------------------------
// Mechanical Turk signing (http://goo.gl/wrzfn)
func sign(auth aws.Auth, service, method, timestamp string, params map[string]string) {
	payload := service + method + timestamp
	hash := hmac.NewSHA1([]byte(auth.SecretKey))
	hash.Write([]byte(payload))
	signature := make([]byte, b64.EncodedLen(hash.Size()))
	b64.Encode(signature, hash.Sum())

	params["Signature"] = string(signature)
}
開發者ID:laslowh,項目名稱:mturk,代碼行數:11,代碼來源:sign.go

示例11: signature

func signature(secret, key, expiration, value string) string {
	hm := hmac.NewSHA1([]byte(secret))
	io.WriteString(hm, key)
	hm.Write([]byte{0})
	io.WriteString(hm, expiration)
	hm.Write([]byte{0})
	io.WriteString(hm, value)
	return hex.EncodeToString(hm.Sum())
}
開發者ID:chillicoder,項目名稱:twister,代碼行數:9,代碼來源:misc.go

示例12: Digest

func Digest(key string, m string) string {
	myhash := hmac.NewSHA1(strings.Bytes(key));
	myhash.Write(strings.Bytes(m));
	signature := bytes.TrimSpace(myhash.Sum());
	digest := make([]byte, base64.StdEncoding.EncodedLen(len(signature)));
	base64.StdEncoding.Encode(digest, signature);
	digest_str := strings.TrimSpace(bytes.NewBuffer(digest).String());
	return digest_str;
}
開發者ID:montsamu,項目名稱:go-twitter-oauth,代碼行數:9,代碼來源:oauth.go

示例13: createSessionId

func createSessionId(username string) string {
	hm := hmac.NewSHA1([]byte(ServerSecret))
	hm.Write([]byte(username))
	hm.Write([]byte(strconv.Itoa64(sessionIdCounter)))
	hm.Write([]byte(strconv.Itoa64(time.Seconds())))
	sessionIdCounter++
	hex := fmt.Sprintf("%02x", hm.Sum())
	return hex
}
開發者ID:AaronO,項目名稱:lightwave,代碼行數:9,代碼來源:session.go

示例14: macSHA1

// macSHA1 returns a macFunction for the given protocol version.
func macSHA1(version uint16, key []byte) macFunction {
	if version == versionSSL30 {
		mac := ssl30MAC{
			h:   sha1.New(),
			key: make([]byte, len(key)),
		}
		copy(mac.key, key)
		return mac
	}
	return tls10MAC{hmac.NewSHA1(key)}
}
開發者ID:Quantumboost,項目名稱:gcc,代碼行數:12,代碼來源:cipher_suites.go

示例15: Sign

func (s *SHA1Signer) Sign(message string, key string) string {
	if s.debug {
		fmt.Println("Signing:" + message)
		fmt.Println("Key:" + key)
	}
	hashfun := hmac.NewSHA1([]byte(key))
	hashfun.Write([]byte(message))
	rawsignature := hashfun.Sum()
	base64signature := make([]byte, base64.StdEncoding.EncodedLen(len(rawsignature)))
	base64.StdEncoding.Encode(base64signature, rawsignature)
	return string(base64signature)
}
開發者ID:newblue,項目名稱:oauth,代碼行數:12,代碼來源:oauth.go


注:本文中的crypto/hmac.NewSHA1函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。