本文整理汇总了Golang中hash/crc32.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: computeOffsets
func computeOffsets(index *nodeIndex, n *trieNode) uint16 {
if n.leaf {
return n.value
}
hasher := crc32.New(crc32.MakeTable(crc32.IEEE))
// We only index continuation bytes.
for i := 0; i < 64; i++ {
var v uint16 = 0
if nn := n.table[0x80+i]; nn != nil {
v = computeOffsets(index, nn)
}
hasher.Write([]byte{uint8(v >> 8), uint8(v)})
}
h := hasher.Sum32()
if n.isInternal() {
v, ok := index.lookupBlockIdx[h]
if !ok {
v = uint16(len(index.lookupBlocks))
index.lookupBlocks = append(index.lookupBlocks, n)
index.lookupBlockIdx[h] = v
}
n.value = v
} else {
v, ok := index.valueBlockIdx[h]
if !ok {
v = uint16(len(index.valueBlocks))
index.valueBlocks = append(index.valueBlocks, n)
index.valueBlockIdx[h] = v
}
n.value = v
}
return n.value
}
示例3: Hash
func (b *backend) Hash(ignores map[IgnoreKey]struct{}) (uint32, error) {
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
b.mu.RLock()
defer b.mu.RUnlock()
err := b.db.View(func(tx *bolt.Tx) error {
c := tx.Cursor()
for next, _ := c.First(); next != nil; next, _ = c.Next() {
b := tx.Bucket(next)
if b == nil {
return fmt.Errorf("cannot get hash of bucket %s", string(next))
}
h.Write(next)
b.ForEach(func(k, v []byte) error {
bk := IgnoreKey{Bucket: string(next), Key: string(k)}
if _, ok := ignores[bk]; !ok {
h.Write(k)
h.Write(v)
}
return nil
})
}
return nil
})
if err != nil {
return 0, err
}
return h.Sum32(), nil
}
示例4: 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
}
示例5: 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
}
示例6: crc32_koopman
func (e *Engine) crc32_koopman() error {
data, err := computeHash(crc32.New(crc32.MakeTable(crc32.Koopman)), e.stack.Pop())
if err == nil {
e.stack.Push(data)
}
return err
}
示例7: sendMessage
func sendMessage(conn io.Writer, message *Message) {
_, err := io.WriteString(conn, "AA") // preamble
if err != nil {
log.Fatal("unable to send data: ", err)
}
data, err := proto.Marshal(message)
if err != nil {
log.Fatal("marshaling error: ", err)
}
err = binary.Write(conn, binary.LittleEndian, int32(len(data)))
if err != nil {
log.Fatal("unable to send data: ", err)
}
_, err = conn.Write(data)
if err != nil {
log.Fatal("unable to send data: ", err)
}
crc := crc32.New(crcTable)
crc.Write(data)
err = binary.Write(conn, binary.LittleEndian, int32(crc.Sum32()))
if err != nil {
log.Fatal("unable to send data: ", err)
}
}
示例8: crc32_castagnoli
func (e *Engine) crc32_castagnoli() error {
data, err := computeHash(crc32.New(crc32.MakeTable(crc32.Castagnoli)), e.stack.Pop())
if err == nil {
e.stack.Push(data)
}
return err
}
示例9: GetFileChecksum
func GetFileChecksum(file *os.File) uint32 {
fileInfo, err := file.Stat()
if err != nil {
log.Println(err)
return 0
}
if fileInfo.Size() > CheckSumMaxSize && CheckSumMaxSize != -1 {
return 0
}
hasher := crc32.New(crc32.MakeTable(crc32.Castagnoli))
byteBuf := make([]byte, ChunkSize)
byteChan := make(chan []byte, ChunkSize)
go func() {
for val := range byteChan {
hasher.Write(val)
}
}()
for done := false; !done; {
numRead, err := file.Read(byteBuf)
if err != nil && err != io.EOF {
log.Println(err)
}
if numRead < ChunkSize {
byteBuf = byteBuf[:numRead]
done = true
}
byteChan <- byteBuf
}
close(byteChan)
return hasher.Sum32()
}
示例10: Hash
func (b *backend) Hash() (uint32, error) {
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
err := b.db.View(func(tx *bolt.Tx) error {
c := tx.Cursor()
for next, _ := c.First(); next != nil; next, _ = c.Next() {
b := tx.Bucket(next)
if b == nil {
return fmt.Errorf("cannot get hash of bucket %s", string(next))
}
h.Write(next)
b.ForEach(func(k, v []byte) error {
h.Write(k)
h.Write(v)
return nil
})
}
return nil
})
if err != nil {
return 0, err
}
return h.Sum32(), nil
}
示例11: DecodePage
func DecodePage(in io.Reader) (Page, error) {
var page Page
err := binary.Read(in, binary.LittleEndian, &page.HeaderFixed)
if err != nil {
return page, err
}
page.Segment_table = make([]uint8, int(page.Page_segments))
_, err = io.ReadFull(in, page.Segment_table)
if err != nil {
return page, err
}
remaining_data := 0
for _, v := range page.Segment_table {
remaining_data += int(v)
}
page.Data = make([]byte, remaining_data)
_, err = io.ReadFull(in, page.Data)
if err != nil {
return page, err
}
// The checksum is made by zeroing the checksum value and CRC-ing the entire page
checksum := page.Crc_checksum
page.Crc_checksum = 0
crc := crc32.New(ogg_table)
binary.Write(crc, binary.LittleEndian, &page.HeaderFixed)
crc.Write(page.Segment_table)
crc.Write(page.Data)
if crc.Sum32() != checksum {
// TODO: Figure out why this CRC isn't working
// return page, os.NewError(fmt.Sprintf("CRC failed: expected %x, got %x.", checksum, crc.Sum32()))
}
return page, nil
}
示例12: Hash
func (s *store) Hash() (uint32, error) {
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
_, err := s.Snapshot(h)
if err != nil {
return 0, err
}
return h.Sum32(), nil
}
示例13: emptyHashes
func emptyHashes() []HashSum {
return []HashSum{
{Name: "md5", hash: md5.New()},
{Name: "sha1", hash: sha1.New()},
{Name: "sha256", hash: sha256.New()},
{Name: "sha512", hash: sha512.New()},
{Name: "adler32", hash: adler32.New()},
{Name: "crc32 (IEEE)", hash: crc32.New(crc32.MakeTable(crc32.IEEE))},
{Name: "crc32 (Castagnoli)", hash: crc32.New(crc32.MakeTable(crc32.Castagnoli))},
{Name: "crc32 (Koopman)", hash: crc32.New(crc32.MakeTable(crc32.Koopman))},
{Name: "crc64 (ISO)", hash: crc64.New(crc64.MakeTable(crc64.ISO))},
{Name: "crc64 (ECMA)", hash: crc64.New(crc64.MakeTable(crc64.ECMA))},
{Name: "fnv32-1", hash: fnv.New32()},
{Name: "fnv32-1a", hash: fnv.New32a()},
{Name: "fnv64-1", hash: fnv.New64()},
{Name: "fnv64-1a", hash: fnv.New64a()},
}
}
示例14: main
func main() {
flag.Parse()
if flag.NArg() < 1 || flag.Arg(0) == "" {
fmt.Printf("usage: crc32 <file>\n")
os.Exit(1)
}
filename := flag.Arg(0)
var poly uint32
switch strings.ToLower(*polynomial) {
case "ieee":
poly = crc32.IEEE
case "castagnoli":
poly = crc32.Castagnoli
case "koopman":
poly = crc32.Koopman
default:
fmt.Printf("unknown -polynomial %s\n", *polynomial)
os.Exit(1)
}
var format string
switch strings.ToLower(*output) {
case "hex":
format = "%x\n"
case "dec":
format = "%d\n"
case "oct":
format = "%o\n"
default:
fmt.Printf("unknown -output %s\n", *output)
os.Exit(1)
}
f, err := os.Open(filename)
if err != nil {
fmt.Printf("%s: %s\n", filename, err)
os.Exit(1)
}
defer f.Close()
// http://blog.vzv.ca/2012/06/crc64-file-hash-in-gogolang.html
h := crc32.New(crc32.MakeTable(poly))
buf := make([]byte, 8192)
read, err := f.Read(buf)
for read > -1 && err == nil {
h.Write(buf)
read, err = f.Read(buf)
}
s := h.Sum32()
fmt.Printf(format, s)
}
示例15: Sum
// Sum - io.Reader based crc helper
func Sum(reader io.Reader) (uint32, error) {
h := crc32.New(castanagoliTable)
var err error
for err == nil {
length := 0
byteBuffer := make([]byte, 1024*1024)
length, err = reader.Read(byteBuffer)
byteBuffer = byteBuffer[0:length]
h.Write(byteBuffer)
}
if err != io.EOF {
return 0, err
}
return h.Sum32(), nil
}