本文整理汇总了Golang中hash.Hash.Write方法的典型用法代码示例。如果您正苦于以下问题:Golang Hash.Write方法的具体用法?Golang Hash.Write怎么用?Golang Hash.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hash.Hash
的用法示例。
在下文中一共展示了Hash.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SHA1Hash
func (id ID) SHA1Hash() (ret ID) {
var h hash.Hash
h = sha1.New()
h.Write(id.AsBytes())
ret = FromBytes(h.Sum(nil))
return ret
}
示例2: finishedSum30
// finishedSum30 calculates the contents of the verify_data member of a SSLv3
// Finished message given the MD5 and SHA1 hashes of a set of handshake
// messages.
func finishedSum30(md5, sha1 hash.Hash, masterSecret []byte, magic []byte) []byte {
md5.Write(magic)
md5.Write(masterSecret)
md5.Write(ssl30Pad1[:])
md5Digest := md5.Sum(nil)
md5.Reset()
md5.Write(masterSecret)
md5.Write(ssl30Pad2[:])
md5.Write(md5Digest)
md5Digest = md5.Sum(nil)
sha1.Write(magic)
sha1.Write(masterSecret)
sha1.Write(ssl30Pad1[:40])
sha1Digest := sha1.Sum(nil)
sha1.Reset()
sha1.Write(masterSecret)
sha1.Write(ssl30Pad2[:40])
sha1.Write(sha1Digest)
sha1Digest = sha1.Sum(nil)
ret := make([]byte, len(md5Digest)+len(sha1Digest))
copy(ret, md5Digest)
copy(ret[len(md5Digest):], sha1Digest)
return ret
}
示例3: passwordToKey
func passwordToKey(proto AuthProtocol, password string, engineId []byte) []byte {
var h hash.Hash
switch proto {
case Md5:
h = md5.New()
case Sha:
h = sha1.New()
}
pass := []byte(password)
plen := len(pass)
for i := mega / plen; i > 0; i-- {
h.Write(pass)
}
remain := mega % plen
if remain > 0 {
h.Write(pass[:remain])
}
ku := h.Sum(nil)
h.Reset()
h.Write(ku)
h.Write(engineId)
h.Write(ku)
return h.Sum(nil)
}
示例4: Checksum
// Checksum returns the checksum of some data, using a specified algorithm.
// It only returns an error when an invalid algorithm is used. The valid ones
// are MD5, SHA1, SHA224, SHA256, SHA384, SHA512, SHA3224, SHA3256, SHA3384,
// and SHA3512.
func Checksum(algorithm string, data []byte) (checksum string, err error) {
// default
var hasher hash.Hash
switch strings.ToUpper(algorithm) {
case "MD5":
hasher = md5.New()
case "SHA1":
hasher = sha1.New()
case "SHA224":
hasher = sha256.New224()
case "SHA256":
hasher = sha256.New()
case "SHA384":
hasher = sha512.New384()
case "SHA512":
hasher = sha512.New()
case "SHA3224":
hasher = sha3.New224()
case "SHA3256":
hasher = sha3.New256()
case "SHA3384":
hasher = sha3.New384()
case "SHA3512":
hasher = sha3.New512()
default:
msg := "Invalid algorithm parameter passed go Checksum: %s"
return checksum, fmt.Errorf(msg, algorithm)
}
hasher.Write(data)
str := hex.EncodeToString(hasher.Sum(nil))
return str, nil
}
示例5: authenticateMessage
func authenticateMessage(signers map[string]Signer, header *Header,
pack *PipelinePack) bool {
digest := header.GetHmac()
if digest != nil {
var key string
signer := fmt.Sprintf("%s_%d", header.GetHmacSigner(),
header.GetHmacKeyVersion())
if s, ok := signers[signer]; ok {
key = s.HmacKey
} else {
return false
}
var hm hash.Hash
switch header.GetHmacHashFunction() {
case Header_MD5:
hm = hmac.New(md5.New, []byte(key))
case Header_SHA1:
hm = hmac.New(sha1.New, []byte(key))
}
hm.Write(pack.MsgBytes)
expectedDigest := hm.Sum(nil)
if bytes.Compare(digest, expectedDigest) != 0 {
return false
}
pack.Signer = header.GetHmacSigner()
}
return true
}
示例6: generateChecksum
func generateChecksum(h hash.Hash, b *[]byte) string {
_, err := h.Write(*b)
if err != nil {
return ""
}
return fmt.Sprintf("%x", h.Sum(nil))
}
示例7: Ripemd
//TODO: test and add to tests
//RIPEMD-160 operation for bitcoin address hashing
func Ripemd(b []byte) []byte {
//ripemd hashing of the sha hash
var h hash.Hash = ripemd160.New()
h.Write(b)
return h.Sum(nil) //return
}
示例8: Fingerprint
// Return the fingerprint of the key in a raw format.
func Fingerprint(pub *SSHPublicKey, hashalgo crypto.Hash) (fpr []byte, err error) {
var h hash.Hash
// The default algorithm for OpenSSH appears to be MD5.
if hashalgo == 0 {
hashalgo = crypto.MD5
}
switch hashalgo {
case crypto.MD5:
h = md5.New()
case crypto.SHA1:
h = sha1.New()
case crypto.SHA256:
h = sha256.New()
default:
return nil, ErrInvalidDigest
}
blob, err := publicToBlob(pub)
if err != nil {
return nil, err
}
h.Write(blob)
return h.Sum(nil), nil
}
示例9: sequentialPacketChannel
// Send len(data) # of sequential packets, hashed with the provided hash.Hash
// down the provided channel
func sequentialPacketChannel(data []byte, hasher hash.Hash) chan *Packet {
// FIXME: Packet size is hard-coded at 1
outchan := make(chan *Packet)
go func() {
for i, _ := range data {
var mbuf [64]byte
hasher.Write(data[i : i+1])
m := hasher.Sum(nil)
for i, _ := range mbuf {
mbuf[i] = m[i]
}
hasher.Reset()
h := PacketHeader{
SequenceN: uint32(i),
Mac: mbuf,
Size: uint32(1),
}
outchan <- &Packet{h, data[i : i+1]}
}
close(outchan)
}()
return outchan
}
示例10: verifyChecksum
// verifyChecksum computes the hash of a file and compares it
// to a checksum. If comparison fails, it returns an error.
func verifyChecksum(fd *os.File, checksum string) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("verifyChecksum() -> %v", e)
}
}()
var h hash.Hash
h = sha256.New()
buf := make([]byte, 4096)
var offset int64 = 0
for {
block, err := fd.ReadAt(buf, offset)
if err != nil && err != io.EOF {
panic(err)
}
if block == 0 {
break
}
h.Write(buf[:block])
offset += int64(block)
}
hexhash := fmt.Sprintf("%x", h.Sum(nil))
if hexhash != checksum {
return fmt.Errorf("Checksum validation failed. Got '%s', Expected '%s'.",
hexhash, checksum)
}
return
}
示例11: newFromHash
// Returns UUID based on hashing of namespace UUID and name.
func newFromHash(h hash.Hash, ns UUID, name string) UUID {
u := UUID{}
h.Write(ns[:])
h.Write([]byte(name))
copy(u[:], h.Sum(nil))
return u
}
示例12: hashCalc
func (otp *OTP) hashCalc(algorithm string) ([8]byte, error) {
var hash_algorithm hash.Hash
tmpseq := STAITC_OTP_OTP_REP_COUNT - (otp.seq % STAITC_OTP_OTP_REP_COUNT)
_string_ := strconv.Itoa(otp.seed) + otp.passphrase
switch otp.mAlgorithm {
case "MD4":
hash_algorithm = md4.New()
case "MD5":
hash_algorithm = md5.New()
case "RIPEMD128":
hash_algorithm = ripemd128.New()
case "RIPEMD160":
hash_algorithm = ripemd160.New()
case "SHA1":
hash_algorithm = sha1.New()
default:
return [8]byte{0, 0, 0, 0, 0, 0, 0, 0}, fmt.Errorf("NoSuchAlgorithmException: %s", otp.mAlgorithm)
}
hash_algorithm.Reset()
hash_algorithm.Write(UCS2_to_UTF8([]byte(_string_)))
otp.hash = hashValueTo8(hash_algorithm.Sum(nil))
for tmpseq > 0 {
hash_algorithm.Reset()
hash_algorithm.Write(otp.hash[:])
otp.hash = hashValueTo8(hash_algorithm.Sum(nil))
tmpseq--
}
return otp.hash, nil
}
示例13: CheckMAC
// CheckMAC returns true if messageMAC is a valid HMAC tag for message.
func CheckMAC(message []byte, messageMAC string, key string) bool {
var err error
var mac hash.Hash
var macdata []byte
var macparts = strings.Split(messageMAC, "=")
macdata, err = hex.DecodeString(macparts[1])
if err != nil {
log.Print("Error decoding hex digest: ", err)
return false
}
switch macparts[0] {
case "md5":
mac = hmac.New(md5.New, []byte(key))
case "sha1":
mac = hmac.New(sha1.New, []byte(key))
case "sha256":
mac = hmac.New(sha256.New, []byte(key))
case "sha512":
mac = hmac.New(sha512.New, []byte(key))
default:
log.Print("Unsupported hash: ", macparts[0])
return false
}
mac.Write(message)
expectedMAC := mac.Sum(nil)
return hmac.Equal(macdata, expectedMAC)
}
示例14: VerifySignature
// VerifySignature returns nil iff sig is a valid signature, made by this
// public key, of the data hashed into signed. signed is mutated by this call.
func (pk *PublicKey) VerifySignature(signed hash.Hash, sig *Signature) (err error) {
if !pk.CanSign() {
return errors.InvalidArgumentError("public key cannot generate signatures")
}
signed.Write(sig.HashSuffix)
hashBytes := signed.Sum(nil)
// NOTE(maxtaco) 2016-08-22
//
// We used to do this:
//
// if hashBytes[0] != sig.HashTag[0] || hashBytes[1] != sig.HashTag[1] {
// return errors.SignatureError("hash tag doesn't match")
// }
//
// But don't do anything in this case. Some GPGs generate bad
// 2-byte hash prefixes, but GPG also doesn't seem to care on
// import. See BrentMaxwell's key. I think it's safe to disable
// this check!
if pk.PubKeyAlgo != sig.PubKeyAlgo {
return errors.InvalidArgumentError("public key and signature use different algorithms")
}
switch pk.PubKeyAlgo {
case PubKeyAlgoRSA, PubKeyAlgoRSASignOnly:
rsaPublicKey, _ := pk.PublicKey.(*rsa.PublicKey)
err = rsa.VerifyPKCS1v15(rsaPublicKey, sig.Hash, hashBytes, sig.RSASignature.bytes)
if err != nil {
return errors.SignatureError("RSA verification failure")
}
return nil
case PubKeyAlgoDSA:
dsaPublicKey, _ := pk.PublicKey.(*dsa.PublicKey)
// Need to truncate hashBytes to match FIPS 186-3 section 4.6.
subgroupSize := (dsaPublicKey.Q.BitLen() + 7) / 8
if len(hashBytes) > subgroupSize {
hashBytes = hashBytes[:subgroupSize]
}
if !dsa.Verify(dsaPublicKey, hashBytes, new(big.Int).SetBytes(sig.DSASigR.bytes), new(big.Int).SetBytes(sig.DSASigS.bytes)) {
return errors.SignatureError("DSA verification failure")
}
return nil
case PubKeyAlgoECDSA:
ecdsaPublicKey := pk.PublicKey.(*ecdsa.PublicKey)
if !ecdsa.Verify(ecdsaPublicKey, hashBytes, new(big.Int).SetBytes(sig.ECDSASigR.bytes), new(big.Int).SetBytes(sig.ECDSASigS.bytes)) {
return errors.SignatureError("ECDSA verification failure")
}
return nil
case PubKeyAlgoEdDSA:
if !pk.edk.Verify(hashBytes, sig.EdDSASigR, sig.EdDSASigS) {
return errors.SignatureError("EdDSA verification failure")
}
return nil
default:
return errors.SignatureError("Unsupported public key algorithm used in signature")
}
panic("unreachable")
}
示例15: MakeSignature
// MakeSignature returns a auth_v4 signature from the `string to sign` variable.
// May be useful for creating v4 requests for services other than DynamoDB.
func MakeSignature(string2sign, zone, service, secret string) string {
kCredentials, _ := cacheable_hmacs(zone, service, secret)
var kSigning_hmac_sha256 hash.Hash = hmac.New(sha256.New, kCredentials)
kSigning_hmac_sha256.Write([]byte(string2sign))
kSigning := kSigning_hmac_sha256.Sum(nil)
return hex.EncodeToString(kSigning)
}