本文整理汇总了Golang中hash/crc64.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetHash
func GetHash(a string) (hash.Hash, error) {
var h hash.Hash
switch a {
case "adler32":
h = adler32.New()
case "crc32", "crc32ieee":
h = crc32.New(crc32.MakeTable(crc32.IEEE))
case "crc32castagnoli":
h = crc32.New(crc32.MakeTable(crc32.Castagnoli))
case "crc32koopman":
h = crc32.New(crc32.MakeTable(crc32.Koopman))
case "crc64", "crc64iso":
h = crc64.New(crc64.MakeTable(crc64.ISO))
case "crc64ecma":
h = crc64.New(crc64.MakeTable(crc64.ECMA))
case "fnv", "fnv32":
h = fnv.New32()
case "fnv32a":
h = fnv.New32a()
case "fnv64":
h = fnv.New64()
case "fnv64a":
h = fnv.New64a()
case "hmac", "hmacsha256":
h = hmac.New(sha256.New, []byte(key))
case "hmacmd5":
h = hmac.New(md5.New, []byte(key))
case "hmacsha1":
h = hmac.New(sha1.New, []byte(key))
case "hmacsha512":
h = hmac.New(sha512.New, []byte(key))
case "md4":
h = md4.New()
case "md5":
h = md5.New()
case "ripemd160":
h = ripemd160.New()
case "sha1":
h = sha1.New()
case "sha224":
h = sha256.New224()
case "sha256":
h = sha256.New()
case "sha384":
h = sha512.New384()
case "sha512":
h = sha512.New()
default:
return nil, errors.New("Invalid algorithm")
}
return h, nil
}
示例2: makeHash
func makeHash(name string) hash.Hash {
switch strings.ToLower(name) {
case "ripemd160":
return ripemd160.New()
case "md4":
return md4.New()
case "md5":
return md5.New()
case "sha1":
return sha1.New()
case "sha256":
return sha256.New()
case "sha384":
return sha512.New384()
case "sha3-224":
return sha3.New224()
case "sha3-256":
return sha3.New256()
case "sha3-384":
return sha3.New384()
case "sha3-512":
return sha3.New512()
case "sha512":
return sha512.New()
case "sha512-224":
return sha512.New512_224()
case "sha512-256":
return sha512.New512_256()
case "crc32-ieee":
return crc32.NewIEEE()
case "crc64-iso":
return crc64.New(crc64.MakeTable(crc64.ISO))
case "crc64-ecma":
return crc64.New(crc64.MakeTable(crc64.ECMA))
case "adler32":
return adler32.New()
case "fnv32":
return fnv.New32()
case "fnv32a":
return fnv.New32a()
case "fnv64":
return fnv.New64()
case "fnv64a":
return fnv.New64a()
case "xor8":
return new(xor8)
case "fletch16":
return &fletch16{}
}
return nil
}
示例3: decStreamHeader
/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
func decStreamHeader(s *xzDec) xzRet {
if string(s.temp.buf[:len(headerMagic)]) != headerMagic {
return xzFormatError
}
if xzCRC32(s.temp.buf[len(headerMagic):len(headerMagic)+2], 0) !=
getLE32(s.temp.buf[len(headerMagic)+2:]) {
return xzDataError
}
if s.temp.buf[len(headerMagic)] != 0 {
return xzOptionsError
}
/*
* Of integrity checks, we support none (Check ID = 0),
* CRC32 (Check ID = 1), CRC64 (Check ID = 4) and SHA256 (Check ID = 10)
* However, we will accept other check types too, but then the check
* won't be verified and a warning (xzUnsupportedCheck) will be given.
*/
s.checkType = xzCheck(s.temp.buf[len(headerMagic)+1])
if s.checkType > xzCheckMax {
return xzOptionsError
}
switch s.checkType {
case xzCheckNone:
// xzCheckNone: no action needed
case xzCheckCRC32:
s.check = crc32.New(xzCRC32Table)
case xzCheckCRC64:
s.check = crc64.New(xzCRC64Table)
case xzCheckSHA256:
s.check = sha256.New()
default:
return xzUnsupportedCheck
}
return xzOK
}
示例4: NewSpecialCollectionCache
func NewSpecialCollectionCache(intent *intents.Intent, demux *Demultiplexer) *SpecialCollectionCache {
return &SpecialCollectionCache{
Intent: intent,
Demux: demux,
hash: crc64.New(crc64.MakeTable(crc64.ECMA)),
}
}
示例5: UniqID
func (p *onePeer) UniqID() uint64 {
h := crc64.New(crctab)
h.Write(p.Ip6[:])
h.Write(p.Ip4[:])
h.Write([]byte{byte(p.Port >> 8), byte(p.Port)})
return h.Sum64()
}
示例6: hashcrc64
func hashcrc64(s []byte) []byte {
var tab = crc64.MakeTable(crc64.ECMA)
h := crc64.New(tab)
h.Write(s)
return h.Sum(nil)
}
示例7: crc64_ecma
func (e *Engine) crc64_ecma() error {
data, err := computeHash(crc64.New(crc64.MakeTable(crc64.ECMA)), e.stack.Pop())
if err == nil {
e.stack.Push(data)
}
return err
}
示例8: archiveWriter
func (a *Archiver) archiveWriter() error {
hash := crc64.New(crc64.MakeTable(crc64.ECMA))
output := io.MultiWriter(a.output, hash)
blockCount := 0
_, err := output.Write(fastArchiverHeader)
if err != nil {
return err
}
for block := range a.blockQueue {
err = block.writeBlock(output)
blockCount += 1
if err == nil && (blockCount%1000) == 0 {
err = writeChecksumBlock(hash, output)
}
if err != nil {
return err
}
}
return writeChecksumBlock(hash, output)
}
示例9: process_file
func process_file(filename string, complete chan Sumlist) {
sumlist := Sumlist{}
sumlist.filename = filename
// Open the file and bail if we fail
infile, err := os.Open(filename)
if err != nil {
log.Printf("Unable to open %s: %s", filename, err)
complete <- sumlist
return
}
defer infile.Close()
// Create the checksum objects
if flag_crc32 {
sumlist.sums = append(sumlist.sums, Checksum{"CRC32", crc32.New(crc32.IEEETable)})
}
if flag_crc64 {
sumlist.sums = append(sumlist.sums, Checksum{"CRC64", crc64.New(crc64.MakeTable(crc64.ISO))})
}
if flag_sha224 {
sumlist.sums = append(sumlist.sums, Checksum{"SHA224", sha256.New224()})
}
if flag_sha256 {
sumlist.sums = append(sumlist.sums, Checksum{"SHA256", sha256.New()})
}
if flag_sha384 {
sumlist.sums = append(sumlist.sums, Checksum{"SHA384", sha512.New384()})
}
if flag_sha512 {
sumlist.sums = append(sumlist.sums, Checksum{"SHA512", sha512.New()})
}
// Create our file reader
reader := bufio.NewReader(infile)
// Start a buffer and loop to read the entire file
buf := make([]byte, 4096)
for {
read_count, err := reader.Read(buf)
// If we get an error that is not EOF, then we have a problem
if err != nil && err != io.EOF {
log.Printf("Unable to open %s: %s", filename, err)
complete <- sumlist
return
}
// If the returned size is zero, we're at the end of the file
if read_count == 0 {
break
}
// Add the buffer contents to the checksum calculation
for _, sum := range sumlist.sums {
sum.hashFunc.Write(buf[:read_count])
}
}
complete <- sumlist
}
示例10: file
func file(fn string) {
ext := path.Ext(fn)
if ext == ".go" {
return
}
in, err := ioutil.ReadFile(fn)
if err != nil {
panic(err)
}
out, err := os.Create(fn + ".go")
if err != nil {
panic(err)
}
defer out.Close()
fmt.Fprintf(out, `package resource
func init() {
Resource[%q] = []byte{`, filepath.Base(fn))
for i, b := range in {
if i == 0 {
fmt.Fprintf(out, `%d`, b)
} else {
fmt.Fprintf(out, `, %d`, b)
}
}
hash := crc64.New(crc64.MakeTable(crc64.ISO))
hash.Write(in)
fmt.Fprintf(out, "}\n\tHash[%q] = \"W/\\\"%d\\\"\"\n}\n", filepath.Base(fn), hash.Sum64())
}
示例11: TestUseCrc
func TestUseCrc(t *testing.T) {
table := crc64.MakeTable(crc64.ISO)
h := crc64.New(table)
input := "someUniqueId"
io.WriteString(h, input)
sum1 := h.Sum64()
h.Reset()
input = "someOtherId"
io.WriteString(h, input)
sum2 := h.Sum64()
if sum1 == sum2 {
t.Errorf("Sums shouldn't match [%x] [%x]\n", sum1, sum2)
t.Fail()
return
}
h.Reset()
input = "someUniqueId"
io.WriteString(h, input)
sum3 := h.Sum64()
if sum1 != sum3 {
t.Errorf("Sums should match [%x] [%x]\n", sum1, sum3)
t.Fail()
return
}
}
示例12: init
func init() {
// Create the hashing function.
hasher := crc64.New(crc64.MakeTable(crc64.ECMA))
h := func(s string) uint64 {
hasher.Reset()
hasher.Write([]byte(s))
return hasher.Sum64()
}
// Get the current user name.
osU, err := user.Current()
u := "UNKNOW"
if err == nil {
u = osU.Username
}
// Create the constant to make build a unique ID.
start := uint64(time.Now().UnixNano())
user := h(u)
pid := uint64(os.Getpid())
// Initialize the channel and blank node type.
nextVal, tBlank = make(chan string, chanSize), Type("/_")
enc := base64.StdEncoding
go func() {
cnt := uint64(0)
for {
bs := []byte(fmt.Sprintf("%x:%x:%x:%x", start, user, pid, cnt))
nextVal <- enc.EncodeToString(bs)
cnt++
}
}()
}
示例13: hash
func (h *HashTable) hash(k []byte) uint64 {
hash := crc64.New(crc64.MakeTable(crc64.ISO))
_, _ = hash.Write(k)
return hash.Sum64() % uint64(len(h.data))
}
示例14: main
func main() {
m := map[string]hash.Hash{
"had": adler32.New(),
"hc32": crc32.NewIEEE(),
"hc64e": crc64.New(crc64.MakeTable(crc64.ECMA)),
"hc64i": crc64.New(crc64.MakeTable(crc64.ISO)),
"hf32": fnv.New32(),
"hf32a": fnv.New32a(),
"hf64": fnv.New64(),
"hf64a": fnv.New64a(),
}
for n, h := range m {
runtime.GC()
testHash(n, h)
}
}
示例15: buildTrie
func (b *builder) buildTrie(t *Trie) uint64 {
n := t.root
// Get the ASCII offset. For the first trie, the ASCII block will be at
// position 0.
hasher := crc64.New(crcTable)
binary.Write(hasher, binary.BigEndian, n.values)
hash := hasher.Sum64()
v, ok := b.asciiBlockIdx[hash]
if !ok {
v = len(b.ValueBlocks)
b.asciiBlockIdx[hash] = v
b.ValueBlocks = append(b.ValueBlocks, n.values[:blockSize], n.values[blockSize:])
if v == 0 {
// Add the zero block at position 2 so that it will be assigned a
// zero reference in the lookup blocks.
// TODO: always do this? This would allow us to remove a check from
// the trie lookup, but at the expense of extra space. Analyze
// performance for unicode/norm.
b.ValueBlocks = append(b.ValueBlocks, make([]uint64, blockSize))
}
}
t.ASCIIIndex = v
// Compute remaining offsets.
t.Checksum = b.computeOffsets(n, true)
// We already subtracted the normal blockOffset from the index. Subtract the
// difference for starter bytes.
t.StarterIndex = n.index.index - (rootBlockOffset - blockOffset)
return t.Checksum
}