本文整理汇总了Golang中hash.Sum函数的典型用法代码示例。如果您正苦于以下问题:Golang Sum函数的具体用法?Golang Sum怎么用?Golang Sum使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Sum函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: calcAes30Params
// Calculates the key and iv for AES decryption given a password and salt.
func calcAes30Params(pass []uint16, salt []byte) (key, iv []byte) {
p := make([]byte, 0, len(pass)*2+len(salt))
for _, v := range pass {
p = append(p, byte(v), byte(v>>8))
}
p = append(p, salt...)
hash := sha1.New()
iv = make([]byte, 16)
s := make([]byte, 0, hash.Size())
for i := 0; i < hashRounds; i++ {
hash.Write(p)
hash.Write([]byte{byte(i), byte(i >> 8), byte(i >> 16)})
if i%(hashRounds/16) == 0 {
s = hash.Sum(s[:0])
iv[i/(hashRounds/16)] = s[4*4+3]
}
}
key = hash.Sum(s[:0])
key = key[:16]
for k := key; len(k) >= 4; k = k[4:] {
k[0], k[1], k[2], k[3] = k[3], k[2], k[1], k[0]
}
return key, iv
}
开发者ID:shazow,项目名称:mog,代码行数:27,代码来源:archive15.go
示例2: PBKDF2
// An implementation of PBKDF2 (Password-Based Key Derivation Function 2) as
// specified in PKCS #5 v2.0 from RSA Laboratorie and in `RFC 2898
// <http://www.ietf.org/rfc/rfc2898.txt>`.
func PBKDF2(hashfunc func([]byte) hash.Hash, password, salt []byte, iterations, keylen int) (key []byte) {
var (
digest []byte
i, j, k, length int
)
key = make([]byte, keylen)
slice := key
hash := hashfunc(password)
hashlen := hash.Size()
scratch := make([]byte, 4)
for keylen > 0 {
if hashlen > keylen {
length = keylen
} else {
length = hashlen
}
i += 1
scratch[0] = byte(i >> 24)
scratch[1] = byte(i >> 16)
scratch[2] = byte(i >> 8)
scratch[3] = byte(i)
hash.Write(salt)
hash.Write(scratch)
digest = hash.Sum()
hash.Reset()
for j = 0; j < length; j++ {
slice[j] = digest[j]
}
for k = 1; k < iterations; k++ {
hash.Write(digest)
digest = hash.Sum()
for j = 0; j < length; j++ {
slice[j] ^= digest[j]
}
hash.Reset()
}
keylen -= length
slice = slice[length:]
}
return
}
开发者ID:gitreview,项目名称:ampify,代码行数:59,代码来源:crypto.go
示例3: Encode
// Encode encodes the given byte slice and returns a token.
func (tok *T) Encode(data []byte) (token []byte, err error) {
if data == nil {
data = []byte{}
}
body := make([]byte, 4+len(data))
now := uint32(time.Now().UTC().Unix())
binary.BigEndian.PutUint32(body, now)
copy(body[4:], data)
body, err = pkcs7Pad(body, aes.BlockSize)
if err != nil {
return nil, err
}
iv := NewKey(aes.BlockSize)
mode := cipher.NewCBCEncrypter(tok.aes, iv)
mode.CryptBlocks(body, body)
hash := tok.hmac()
// size = len(iv + aesblocks + signature)
token = make([]byte, len(iv)+len(body)+hash.Size())
copy(token, iv)
offset := len(iv)
copy(token[offset:], body)
offset += len(body)
hash.Write(token[:offset])
copy(token[offset:], hash.Sum(nil))
b := make([]byte, base64.RawURLEncoding.EncodedLen(len(token)))
base64.RawURLEncoding.Encode(b, token)
return b, nil
}
开发者ID:go-web,项目名称:tokenizer,代码行数:29,代码来源:token.go
示例4: Hash
func Hash(fileName string, newHash func() hash.Hash) ([]byte, error) {
hash := newHash()
file, err := os.Open(fileName)
if err != nil {
return nil, common.TraceError(err)
}
defer file.Close()
temp := make([]byte, 4096)
for {
nn, err := file.Read(temp)
if err == io.EOF {
break
}
if err != nil {
return nil, common.TraceError(err)
}
hash.Write(temp[0:nn])
}
return hash.Sum(nil), nil
}
开发者ID:NatTuck,项目名称:defunct-fogsync,代码行数:25,代码来源:crypto.go
示例5: ObjectPut
// ObjectPut creates or updates the path in the container from
// contents. contents should be an open io.Reader which will have all
// its contents read.
//
// This is a low level interface.
//
// If checkHash is True then it will calculate the MD5 Hash of the
// file as it is being uploaded and check it against that returned
// from the server. If it is wrong then it will return
// ObjectCorrupted.
//
// If you know the MD5 hash of the object ahead of time then set the
// Hash parameter and it will be sent to the server (as an Etag
// header) and the server will check the MD5 itself after the upload,
// and this will return ObjectCorrupted if it is incorrect.
//
// If you don't want any error protection (not recommended) then set
// checkHash to false and Hash to "".
//
// If contentType is set it will be used, otherwise one will be
// guessed from objectName using mime.TypeByExtension
func (c *Connection) ObjectPut(container string, objectName string, contents io.Reader, checkHash bool, Hash string, contentType string, h Headers) (headers Headers, err error) {
extraHeaders := objectPutHeaders(objectName, &checkHash, Hash, contentType, h)
hash := md5.New()
var body io.Reader = contents
if checkHash {
body = io.TeeReader(contents, hash)
}
_, headers, err = c.storage(RequestOpts{
Container: container,
ObjectName: objectName,
Operation: "PUT",
Headers: extraHeaders,
Body: body,
NoResponse: true,
ErrorMap: objectErrorMap,
})
if err != nil {
return
}
if checkHash {
receivedMd5 := strings.ToLower(headers["Etag"])
calculatedMd5 := fmt.Sprintf("%x", hash.Sum(nil))
if receivedMd5 != calculatedMd5 {
err = ObjectCorrupted
return
}
}
return
}
开发者ID:joeshaw,项目名称:swift,代码行数:50,代码来源:swift.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng