本文整理汇总了Golang中hash/crc32.ChecksumIEEE函数的典型用法代码示例。如果您正苦于以下问题:Golang ChecksumIEEE函数的具体用法?Golang ChecksumIEEE怎么用?Golang ChecksumIEEE使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ChecksumIEEE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: hashKey
func (c *Consistent) hashKey(key string) uint32 {
if len(key) < 64 {
copy(c.scratch[:], key)
return crc32.ChecksumIEEE(c.scratch[:len(key)])
}
return crc32.ChecksumIEEE([]byte(key))
}
示例2: compareFiles
func compareFiles(srcPath, destPath string) (bool, error) {
src, err := os.Open(srcPath)
if err != nil {
return false, fmt.Errorf("open source failed: %s", err)
}
defer src.Close()
dest, err := os.Open(destPath)
if err != nil {
return false, fmt.Errorf("open dest failed: %s", err)
}
defer dest.Close()
srcContents, err := ioutil.ReadAll(src)
if err != nil {
return false, fmt.Errorf("read source failed: %s", err)
}
destContents, err := ioutil.ReadAll(dest)
if err != nil {
return false, fmt.Errorf("read dest failed: %s", err)
}
contentsHash := fmt.Sprintf("%x", crc32.ChecksumIEEE(srcContents))
srcHash := fmt.Sprintf("%x", crc32.ChecksumIEEE(destContents))
return srcHash == contentsHash, nil
}
示例3: Apply
// Apply applies the patch data to the given base.
func Apply(base []byte, patch *common.PatchData, skipCRC bool) ([]byte, error) {
if uint64(len(base)) != patch.InputFileSize {
return nil, errors.New("Base file did not have expected size.")
}
if !skipCRC && crc32.ChecksumIEEE(base) != patch.InputChecksum {
return nil, errors.New("Base file did not have expected checksum")
}
output := make([]byte, patch.OutputFileSize)
copy(output, base)
pointer := 0
for _, block := range patch.PatchBlocks {
pointer += int(block.RelativeOffset)
for _, b := range block.Data {
if pointer >= len(base) {
output[pointer] = b
} else {
output[pointer] = base[pointer] ^ b
}
pointer++
}
pointer++
}
if !skipCRC && crc32.ChecksumIEEE(output) != patch.OutputChecksum {
return nil, errors.New("Patch result did not have expected checksum")
}
return output, nil
}
示例4: getCrc
func getCrc(key string) uint32 {
if len(key) < 64 {
var scratch [64]byte
copy(scratch[:], key)
return crc32.ChecksumIEEE(scratch[:len(key)])
}
return crc32.ChecksumIEEE([]byte(key))
}
示例5: hashStr
// Hash function
func (c *Consistent) hashStr(key string) uint32 {
if len(key) < 64 {
var scratch [64]byte
copy(scratch[:], key)
return crc32.ChecksumIEEE(scratch[:len(key)])
}
return crc32.ChecksumIEEE([]byte(key))
}
示例6: main
func main() {
cs := crc32.ChecksumIEEE([]byte("1234667"))
fmt.Println(cs)
cs = crc32.ChecksumIEEE([]byte("dfdfdfdfdf"))
fmt.Println(cs)
}
示例7: hashKey
func (c *Consistent) hashKey(key string) uint32 {
//
//log.Println("key string:", key)
if len(key) < 64 {
var scratch [64]byte
copy(scratch[:], key)
//log.Fatal(len(key), scratch)
return crc32.ChecksumIEEE(scratch[:len(key)])
}
return crc32.ChecksumIEEE([]byte(key))
}
示例8: TestNew_resources
func TestNew_resources(t *testing.T) {
assert := assert.New(t)
r, err := os.Open("testdata/wikipedia.webarchive")
require.NoError(t, err)
a, err := New(r)
assert.Equal(uint32(0x7a24addb), crc32.ChecksumIEEE(a.Content.Data))
assert.Equal("text/html", a.Content.MIMEType)
assert.Equal("UTF-8", a.Content.Encoding)
assert.Equal("https://en.wikipedia.org/wiki/Webarchive", a.Content.URL)
assert.Empty(a.Content.FrameName)
assert.Len(a.Resources, 13)
assert.NoError(err)
resources := []struct {
data uint32
mime string
enc string
url uint32
resp uint32
}{
{0xe0352bc6, "text/css", "utf-8", 0xa88c8657, 0x3e495c5f},
{0xd5fee4ca, "text/css", "utf-8", 0x348faf99, 0xe1b3e4d0},
{0xab5b0999, "text/javascript", "utf-8", 0xe9d253b4, 0x58d61084},
{0xf3c27e95, "text/javascript", "utf-8", 0xfeaf026a, 0x96e53929},
{0x59637a, "image/png", "", 0xbd73e04a, 0x6dd3aad5},
{0xf79b5fc7, "image/png", "", 0x8d210858, 0xfd3b31c4},
{0xfbea27a5, "image/jpeg", "", 0x482f361f, 0xdf0d81fd},
{0xdae1d80a, "image/png", "", 0x2ab8618b, 0x570a3987},
{0x285bcd96, "image/png", "", 0x5903bafa, 0xad214977},
{0xc296a07a, "image/png", "", 0x23103ba7, 0xd845f71},
{0x1af78e35, "image/png", "", 0x9af068df, 0x19fc7a6},
{0x7c4edfa, "image/png", "", 0x7de2a6e8, 0x36084aec},
{0x9885885, "text/css", "utf-8", 0x8c2feaff, 0xb01023b6},
}
for i := 0; i < 13; i++ {
sr := a.Resources[i]
r := resources[i]
s := fmt.Sprintf("subresource %d", i)
assert.Equal(r.data, crc32.ChecksumIEEE(sr.Data), s)
assert.Equal(r.mime, sr.MIMEType, s)
assert.Equal(r.enc, sr.Encoding, s)
assert.Equal(r.url, crc32.ChecksumIEEE([]byte(sr.URL)), s)
assert.Equal(r.resp, crc32.ChecksumIEEE(sr.Response), s)
}
}
示例9: HashValue
func HashValue(value interface{}) uint64 {
switch val := value.(type) {
case int:
return uint64(val)
case uint64:
return uint64(val)
case int64:
return uint64(val)
case string:
return uint64(crc32.ChecksumIEEE(hack.Slice(val)))
case []byte:
return uint64(crc32.ChecksumIEEE(val))
}
panic(NewKeyError("Unexpected key variable type %T", value))
}
示例10: TestEncodeDecodeEntry
func TestEncodeDecodeEntry(t *testing.T) {
/**
crc32 : tStamp : ksz : valueSz : key : value
4 : 4 : 4 : 4 : xxxx : xxxx
**/
//func encodeEntry(tStamp, keySize, valueSize uint32, key, value []byte) []byte {
// EncodeEntry
tStamp := uint32(time.Now().Unix())
key := []byte("Foo")
value := []byte("Bar")
ksz := uint32(len(key))
valuesz := uint32(len(value))
buf := make([]byte, HeaderSize+ksz+valuesz)
binary.LittleEndian.PutUint32(buf[4:8], tStamp)
binary.LittleEndian.PutUint32(buf[8:12], ksz)
binary.LittleEndian.PutUint32(buf[12:16], valuesz)
copy(buf[16:(16+ksz)], key)
copy(buf[(16+ksz):(16+ksz+valuesz)], value)
c32 := crc32.ChecksumIEEE(buf[4:])
binary.LittleEndian.PutUint32(buf[0:4], uint32(c32))
// Test decode
ksz = binary.LittleEndian.Uint32(buf[8:12])
valuesz = binary.LittleEndian.Uint32(buf[12:16])
tStamp = binary.LittleEndian.Uint32(buf[4:8])
c32 = binary.LittleEndian.Uint32(buf[:4])
assert.Equal(t, binary.LittleEndian.Uint32(buf[0:4]), c32)
assert.Equal(t, binary.LittleEndian.Uint32(buf[4:8]), tStamp)
assert.Equal(t, binary.LittleEndian.Uint32(buf[8:12]), ksz)
assert.Equal(t, binary.LittleEndian.Uint32(buf[12:16]), valuesz)
assert.Equal(t, buf[HeaderSize:(HeaderSize+ksz)], key)
assert.Equal(t, buf[(HeaderSize+ksz):(HeaderSize+ksz+valuesz)], value)
// EncodeEntry , ksz = 0, valueSz = 0
ksz = uint32(0)
valuesz = uint32(0)
buf = make([]byte, HeaderSize+ksz+valuesz, HeaderSize+ksz+valuesz)
binary.LittleEndian.PutUint32(buf[4:8], tStamp)
binary.LittleEndian.PutUint32(buf[8:12], ksz)
binary.LittleEndian.PutUint32(buf[12:16], valuesz)
c32 = crc32.ChecksumIEEE(buf[4:])
binary.LittleEndian.PutUint32(buf[0:4], c32)
// decodeEntry, ksz =0, valueSz = 0
assert.Equal(t, binary.LittleEndian.Uint32(buf[0:4]), c32)
assert.Equal(t, binary.LittleEndian.Uint32(buf[4:8]), tStamp)
assert.Equal(t, binary.LittleEndian.Uint32(buf[8:12]), ksz)
assert.Equal(t, binary.LittleEndian.Uint32(buf[12:16]), valuesz)
}
示例11: Read
func Read(r io.Reader) (*Message, error) {
var incomingHeader header
err := binary.Read(r, binary.LittleEndian, &incomingHeader)
if err != nil {
return nil, err
}
if incomingHeader.ContentLength > MaximumMessageSize {
return nil, fmt.Errorf("message too large: requested %d bytes but max is %d", incomingHeader.ContentLength, MaximumMessageSize)
}
msg := new(Message)
data := make([]byte, incomingHeader.ContentLength)
// Keep reading so we are resilient to IP fragmentation along the path.
for n := uint32(0); n < incomingHeader.ContentLength; {
nRead, err := r.Read(data[n:])
if err != nil {
return nil, err
}
n += uint32(nRead)
}
// Checksum the incoming data so that transmission errors can be dealt with.
check := crc32.ChecksumIEEE(data)
if check != incomingHeader.Checksum {
return nil, ErrCorruptedMessage
}
err = proto.Unmarshal(data[0:incomingHeader.ContentLength], msg)
return msg, err
}
示例12: Write
// Write marshals a Message into the proper on-wire format and sends it
// to the remote system.
func Write(w io.Writer, msg *Message) error {
buf, err := proto.Marshal(msg)
if err != nil {
return err
}
// For now we just compute the length as we don't have tests.
bufHeader := &header{
ContentLength: uint32(len(buf)),
Checksum: crc32.ChecksumIEEE(buf),
Reserved: 0,
}
err = binary.Write(w, binary.LittleEndian, bufHeader)
if err != nil {
return err
}
for totalWritten := 0; totalWritten < len(buf); {
nWritten, err := w.Write(buf)
if err != nil {
return err
}
totalWritten += nWritten
}
return nil
}
示例13: Marshal
func (t *CRCMessage) Marshal(w *wipro.Writer) {
offset := len(w.B)
w.WriteUint32(t.CRC)
start := len(w.B)
t.Message.Marshal(w)
w.SetUint32(offset, crc32.ChecksumIEEE(w.B[start:]))
}
示例14: MakeHashes
func MakeHashes(word string, methods []string) map[string]string {
hashes := make(map[string]string)
for _, method := range methods {
var hash string
// TODO: Replace this with reflection
// for easier extensibility
switch method {
case "crc32":
hashInt := uint64(crc32.ChecksumIEEE([]byte(word)))
hash = strconv.FormatUint(hashInt, 16)
case "md5":
hash = fmt.Sprintf("%x", md5.Sum([]byte(word)))
case "sha1":
hash = fmt.Sprintf("%x", sha1.Sum([]byte(word)))
case "sha256":
hash = fmt.Sprintf("%x", sha256.Sum256([]byte(word)))
}
hashes[method] = hash
}
return hashes
}
示例15: save
// save writes the snapshot to file.
func (ss *Snapshot) save() error {
// Open the file for writing.
file, err := os.OpenFile(ss.Path, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer file.Close()
// Serialize to JSON.
b, err := json.Marshal(ss)
if err != nil {
return err
}
// Generate checksum and write it to disk.
checksum := crc32.ChecksumIEEE(b)
if _, err = fmt.Fprintf(file, "%08x\n", checksum); err != nil {
return err
}
// Write the snapshot to disk.
if _, err = file.Write(b); err != nil {
return err
}
// Ensure that the snapshot has been flushed to disk before continuing.
if err := file.Sync(); err != nil {
return err
}
return nil
}