本文整理汇总了Golang中hash.Hash.Size方法的典型用法代码示例。如果您正苦于以下问题:Golang Hash.Size方法的具体用法?Golang Hash.Size怎么用?Golang Hash.Size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hash.Hash
的用法示例。
在下文中一共展示了Hash.Size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Tweak
// Tweak generates a new tweak from the mode, hash, salt length (in
// bytes), and any additional data. It provides additional information
// that will complicate an attacker's efforts, and allows a system to
// differentiate between different uses of the Catena function's output.
func Tweak(mode byte, H hash.Hash, saltLen int, ad []byte) ([]byte, error) {
if mode != ModePassHash && mode != ModeKeyDerivation {
return nil, ErrInvalidTweakMode
}
hashLen := H.Size()
tweakLen := 5 + hashLen
var t = make([]byte, 1, tweakLen)
t[0] = mode
var tmp uint16 = uint16(H.Size() * 8)
high := byte(tmp >> 8)
low := byte(tmp << 8 >> 8)
t = append(t, high)
t = append(t, low)
tmp = uint16(saltLen * 8)
high = byte(tmp >> 8)
low = byte(tmp << 8 >> 8)
t = append(t, high)
t = append(t, low)
H.Reset()
H.Write(ad)
t = append(t, H.Sum(nil)...)
H.Reset()
return t, nil
}
示例2: Encode
func Encode(k int, h hash.Hash, value []byte, output int) (enc []byte, s [][]byte) {
s0 := make([]byte, h.Size())
n := len(value)
blockcount := n / k
s = make([][]byte, blockcount)
for i := 0; i < blockcount; i++ {
h.Reset()
if i == 0 {
h.Write(s0)
} else {
h.Write(s[i-1])
}
h.Write(value[i*k : (i+1)*k])
s[i] = h.Sum(make([]byte, 0, h.Size()))
}
rng := make([](*RNG), len(s))
for i := 0; i < len(s); i++ {
rng[i] = NewRNG(h, s[i])
}
enc = make([]byte, output)
for i := 0; i < output; i++ {
enc[i] = rng[i%blockcount].Next()
}
return
}
示例3: decode
// decode uses the given block cipher (in CTR mode) to decrypt the
// data, and validate the hash. If hash validation fails, an error is
// returned.
func decode(block cipher.Block, hmac hash.Hash, ciphertext []byte) ([]byte, error) {
if len(ciphertext) < 2*block.BlockSize()+hmac.Size() {
return nil, LenError
}
receivedHmac := ciphertext[len(ciphertext)-hmac.Size():]
ciphertext = ciphertext[:len(ciphertext)-hmac.Size()]
hmac.Write(ciphertext)
if subtle.ConstantTimeCompare(hmac.Sum(nil), receivedHmac) != 1 {
return nil, HashError
}
// split the iv and session bytes
iv := ciphertext[len(ciphertext)-block.BlockSize():]
session := ciphertext[:len(ciphertext)-block.BlockSize()]
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(session, session)
// skip past the iv
session = session[block.BlockSize():]
return session, nil
}
示例4: DeriveConcatKDF
// DeriveConcatKDF implements NIST SP 800-56A Concatenation Key Derivation Function. Derives
// key material of keydatalen bits size given Z (sharedSecret), OtherInfo (AlgorithmID |
// PartyUInfo | PartyVInfo | SuppPubInfo | SuppPrivInfo) and hash function
func DeriveConcatKDF(keydatalen int, sharedSecret, algId, partyUInfo, partyVInfo, suppPubInfo, suppPrivInfo []byte, h hash.Hash) []byte {
otherInfo := arrays.Concat(algId, partyUInfo, partyVInfo, suppPubInfo, suppPrivInfo)
keyLenBytes := keydatalen >> 3
reps := int(math.Ceil(float64(keyLenBytes) / float64(h.Size())))
if reps > MaxInt {
panic("kdf.DeriveConcatKDF: too much iterations (more than 2^32-1).")
}
dk := make([]byte, 0, keyLenBytes)
for counter := 1; counter <= reps; counter++ {
h.Reset()
counterBytes := arrays.UInt32ToBytes(uint32(counter))
h.Write(counterBytes)
h.Write(sharedSecret)
h.Write(otherInfo)
dk = h.Sum(dk)
}
return dk[:keyLenBytes]
}
示例5: EncryptOAEP
// EncryptOAEP encrypts the given message with RSA-OAEP.
// The message must be no longer than the length of the public modulus less
// twice the hash length plus 2.
func EncryptOAEP(hash hash.Hash, rand io.Reader, pub *PublicKey, msg []byte, label []byte) (out []byte, err os.Error) {
hash.Reset()
k := (pub.N.Len() + 7) / 8
if len(msg) > k-2*hash.Size()-2 {
err = MessageTooLongError{}
return
}
hash.Write(label)
lHash := hash.Sum()
hash.Reset()
em := make([]byte, k)
seed := em[1 : 1+hash.Size()]
db := em[1+hash.Size():]
copy(db[0:hash.Size()], lHash)
db[len(db)-len(msg)-1] = 1
copy(db[len(db)-len(msg):], msg)
_, err = io.ReadFull(rand, seed)
if err != nil {
return
}
mgf1XOR(db, hash, seed)
mgf1XOR(seed, hash, db)
m := new(big.Int)
m.SetBytes(em)
c := encrypt(new(big.Int), pub, m)
out = c.Bytes()
return
}
示例6: Decrypt
func (s *descbc) Decrypt(salt []byte, algo, usage int, data []byte) ([]byte, error) {
var h hash.Hash
switch algo {
case cryptDesCbcMd5:
h = md5.New()
case cryptDesCbcMd4:
h = md4.New()
default:
return nil, ErrProtocol
}
if (len(data) & 7) != 0 {
return nil, ErrProtocol
}
iv := [8]byte{}
b, _ := des.NewCipher(s.key)
c := cipher.NewCBCDecrypter(b, iv[:])
c.CryptBlocks(data, data)
chk := make([]byte, h.Size())
h.Write(data[:8])
h.Write(chk) // Just need h.Size() zero bytes instead of the checksum
h.Write(data[8+len(chk):])
h.Sum(chk[:0])
if subtle.ConstantTimeCompare(chk, data[8:8+len(chk)]) != 1 {
return nil, ErrProtocol
}
return data[8+len(chk):], nil
}
示例7: NewHashAppendReader
func NewHashAppendReader(r io.Reader, h hash.Hash) *HashAppendReader {
return &HashAppendReader{
h: h,
r: io.TeeReader(r, h),
sum: make([]byte, 0, h.Size()),
}
}
示例8: JsapiSign
// jssdk 支付签名, signType 只支持 "MD5", "SHA1", 传入其他的值会 panic.
func JsapiSign(appId, timeStamp, nonceStr, packageStr, signType string, apiKey string) string {
var h hash.Hash
switch signType {
case "MD5":
h = md5.New()
case "SHA1":
h = sha1.New()
default:
panic("unsupported signType")
}
bufw := bufio.NewWriterSize(h, 128)
// appId
// nonceStr
// package
// signType
// timeStamp
bufw.WriteString("appId=")
bufw.WriteString(appId)
bufw.WriteString("&nonceStr=")
bufw.WriteString(nonceStr)
bufw.WriteString("&package=")
bufw.WriteString(packageStr)
bufw.WriteString("&signType=")
bufw.WriteString(signType)
bufw.WriteString("&timeStamp=")
bufw.WriteString(timeStamp)
bufw.WriteString("&key=")
bufw.WriteString(apiKey)
bufw.Flush()
signature := make([]byte, hex.EncodedLen(h.Size()))
hex.Encode(signature, h.Sum(nil))
return string(bytes.ToUpper(signature))
}
示例9: NewRNG
func NewRNG(h hash.Hash, seed []byte) (r *RNG) {
r = new(RNG)
r.i = 3610617884
r.h = h
r.buffer = make([]byte, 0, h.Size())
r.seed = seed
return
}
示例10: GenSessionId
func GenSessionId() string {
var h hash.Hash = sessHash()
h.Write(GenRandomBytes(sessEnthropy))
h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10)))
id := make([]byte, 0, h.Size())
id = h.Sum(id)
return base64.StdEncoding.EncodeToString(id)
}
示例11: NewHashAppendWriter
func NewHashAppendWriter(w io.Writer, h hash.Hash) *HashAppendWriter {
return &HashAppendWriter{
h: h,
w: io.MultiWriter(w, h),
origWr: w,
sum: make([]byte, 0, h.Size()),
}
}
示例12: GoId
// GoId creates a UUID object based on timestamps and a hash.
// It will truncate any bytes past the length of the initial hash.
// This creates a UUID based on a Namespace, UniqueName and an existing
// hash.
func GoId(pNs UUID, pName UniqueName, pHash hash.Hash) UUID {
o := new(UUIDStruct)
o.size = pHash.Size()
Digest(o, pNs, pName, pHash)
now := currentUUIDTimestamp()
sequence := uint16(seed.Int()) & 0x3FFF
return formatGoId(o, now, 15, ReservedFuture, sequence)
}
示例13: calculateToken
// this is the function which calculates the HTOP code
func calculateToken(counter []byte, digits int, h hash.Hash) string {
h.Write(counter)
hashResult := h.Sum(nil)
result := truncateHash(hashResult, h.Size())
mod := int32(result % int64(math.Pow10(digits)))
fmtStr := fmt.Sprintf("%%0%dd", digits)
return fmt.Sprintf(fmtStr, mod)
}
示例14: hashWithPrefix
func hashWithPrefix(out []byte, prefix byte, in []byte, h hash.Hash) {
h.Reset()
var p [1]byte
p[0] = prefix
h.Write(p[:])
h.Write(in)
if len(out) == h.Size() {
h.Sum(out[:0])
} else {
digest := h.Sum(nil)
copy(out, digest)
}
}
示例15: testWrite
func testWrite(msg string, t *testing.T, h hash.Hash, c *Config) {
var msg1 []byte
msg0 := make([]byte, 64)
for i := range msg0 {
h.Write(msg0[:i])
msg1 = append(msg1, msg0[:i]...)
}
tag0 := h.Sum(nil)
tag1 := Sum(msg1, h.Size(), c)
if !bytes.Equal(tag0, tag1) {
t.Fatalf("%s\nSum differ from Sum\n Sum: %s \n skein.Sum: %s", msg, hex.EncodeToString(tag0), hex.EncodeToString(tag1))
}
}