func main() { useSHA := flag.String("use-sha", "", "<default>=no hashing, 1=sha1, 256=sha256, 512=sha512, blake=blake2b") useBH := flag.Bool("use-bh", false, "whether we buzhash the bytes") flag.Parse() flag.Usage = func() { fmt.Printf("%s <big-file>\n", os.Args[0]) flag.PrintDefaults() return } if len(flag.Args()) < 1 { flag.Usage() return } p := flag.Args()[0] bh := buzhash.NewBuzHash(64 * 8) f, _ := os.Open(p) defer f.Close() t0 := time.Now() buf := make([]byte, 4*1024) l := uint64(0) var h hash.Hash if *useSHA == "1" { h = sha1.New() } else if *useSHA == "256" { h = sha256.New() } else if *useSHA == "512" { h = sha512.New() } else if *useSHA == "blake" { h = blake2.NewBlake2B() } for { n, err := f.Read(buf) l += uint64(n) if err == io.EOF { break } s := buf[:n] if h != nil { h.Write(s) } if *useBH { bh.Write(s) } } t1 := time.Now() d := t1.Sub(t0) fmt.Printf("Read %s in %s (%s/s)\n", humanize.Bytes(l), d, humanize.Bytes(uint64(float64(l)/d.Seconds()))) digest := []byte{} if h != nil { fmt.Printf("%x\n", h.Sum(digest)) } }
func main() { flag.Parse() hashAlgorithm := sha1.New() if *sha1Flag { hashAlgorithm = sha1.New() } if *md5Flag { hashAlgorithm = md5.New() } if *sha256Flag { hashAlgorithm = sha256.New() } if *sha384Flag { hashAlgorithm = sha512.New384() } if *sha3256Flag { hashAlgorithm = sha3.New256() } if *sha3384Flag { hashAlgorithm = sha3.New384() } if *sha3512Flag { hashAlgorithm = sha3.New512() } if *whirlpoolFlag { hashAlgorithm = whirlpool.New() } if *blakeFlag { hashAlgorithm = blake2.NewBlake2B() } if *ripemd160Flag { hashAlgorithm = ripemd160.New() } for _, fileName := range flag.Args() { f, _ := os.Open(fileName) defer f.Close() hashAlgorithm.Reset() output := genericHashFile(f, hashAlgorithm) if *b64Flag { r64Output := base64.StdEncoding.EncodeToString(output) fmt.Printf("%s %s\n", r64Output, fileName) } else { fmt.Printf("%x %s\n", output, fileName) } } }
func UploadFile(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Write file to ./data%v\n", r.URL.Path) dir := path.Dir("./data" + r.URL.Path) if os.MkdirAll(dir, os.ModeDir|os.ModePerm) != nil { http.Error(w, "Failed to create directory.", 500) return } var err error r.ParseMultipartForm(32 << 20) in, _, err := r.FormFile("file") if err != nil { http.Error(w, "Failed to read file from multipart form.", 500) return } defer in.Close() var out *os.File out, err = os.OpenFile("./data"+r.URL.Path, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { http.Error(w, "Failed to write file.", 500) return } defer out.Close() hash := blake2.NewBlake2B() writer := io.MultiWriter(out, hash) io.Copy(writer, in) d := hash.Sum(nil) fmt.Fprintf(w, "Hash is %X\n", d) }
func sum(data []byte) []byte { hash := blake2.NewBlake2B() return hash.Sum(data) }
// Hash a key and return the first 16 hex chars of its blake2b hash. // basically: Blake2b(key).HexString[:16] func BlakeKeyHash(key ds.Key) string { h := blake2.NewBlake2B() h.Write(key.Bytes()) d := h.Sum(nil) return fmt.Sprintf("%x", d)[:16] }