本文整理汇总了Golang中hash.Hash类的典型用法代码示例。如果您正苦于以下问题:Golang Hash类的具体用法?Golang Hash怎么用?Golang Hash使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Hash类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: 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
}
开发者ID:tudalex,项目名称:mig,代码行数:30,代码来源:upgrade.go
示例2: Iterated
// Iterated writes to out the result of computing the Iterated and Salted S2K
// function (RFC 4880, section 3.7.1.3) using the given hash, input passphrase,
// salt and iteration count.
func Iterated(out []byte, h hash.Hash, in []byte, salt []byte, count int) {
combined := make([]byte, len(in)+len(salt))
copy(combined, salt)
copy(combined[len(salt):], in)
if count < len(combined) {
count = len(combined)
}
done := 0
var digest []byte
for i := 0; done < len(out); i++ {
h.Reset()
for j := 0; j < i; j++ {
h.Write(zero[:])
}
written := 0
for written < count {
if written+len(combined) > count {
todo := count - written
h.Write(combined[:todo])
written = count
} else {
h.Write(combined)
written += len(combined)
}
}
digest = h.Sum(digest[:0])
n := copy(out[done:], digest)
done += n
}
}
开发者ID:postfix,项目名称:name_pending,代码行数:35,代码来源:s2k.go
示例3: RefFromHash
// RefFromHash returns a blobref representing the given hash.
// It panics if the hash isn't of a known type.
func RefFromHash(h hash.Hash) Ref {
meta, ok := metaFromType[reflect.TypeOf(h)]
if !ok {
panic(fmt.Sprintf("Currently-unsupported hash type %T", h))
}
return Ref{meta.ctor(h.Sum(nil))}
}
开发者ID:JayBlaze420,项目名称:camlistore,代码行数:9,代码来源:ref.go
示例4: authenticateMessage
// Returns true if the provided message is unsigned or has a valid signature
// from one of the provided signers.
func authenticateMessage(signers map[string]Signer, header *Header, msg []byte) 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(msg)
expectedDigest := hm.Sum(nil)
if subtle.ConstantTimeCompare(digest, expectedDigest) != 1 {
return false
}
}
return true
}
开发者ID:RogerBai,项目名称:heka,代码行数:29,代码来源:net_utils.go
示例5: LoadPost
func LoadPost(ctx *web.Context, val string) {
username := ctx.Params["username"]
password := ctx.Params["password"]
salt := strconv.Itoa64(time.Nanoseconds()) + username
var h hash.Hash = sha256.New()
h.Write([]byte(password + salt))
s, _err := conn.Prepare("INSERT INTO users VALUES(NULL, ?, ?, ?)")
utils.ReportErr(_err)
s.Exec(username, string(h.Sum()), salt)
s.Finalize()
conn.Close()
sidebar := utils.Loadmustache("admin.mustache", &map[string]string{})
//TESTING, REMOVE LATER
script := "<script type=\"text/javascript\" src=\"../inc/adminref.js\"></script>"
content := "Welcome to the admin panel, use the control box on your right to control the site content"
//ENDTESTING
mapping := map[string]string{"css": "../inc/site.css",
"title": "Proggin: Admin panel",
"sidebar": sidebar,
"content": content,
"script": script}
output := utils.Loadmustache("frame.mustache", &mapping)
ctx.WriteString(output)
}
开发者ID:Chownie,项目名称:Proggin,代码行数:31,代码来源:newuser.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng