func getHashSHA3(bitsize int) (hash.Hash, error) { switch bitsize { case 224: return sha3.New224(), nil case 256: return sha3.New256(), nil case 384: return sha3.New384(), nil case 512: return sha3.New512(), nil case 521: return sha3.New512(), nil default: return nil, fmt.Errorf("Invalid bitsize. It was [%d]. Expected [224, 256, 384, 512, 521]", bitsize) } }
// ServeIzkp returns an http.Handler that reads an input file and // computes an interactive zero-knowledge proof-of-posession protocol. // (This is completely unused, but isn't it cool?) func ServeIzkp(fn string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { b, err := ioutil.ReadFile(fn) if err != nil { glog.Errorf("error reading file %s: %s", fn, err) w.WriteHeader(http.StatusInternalServerError) return } chalString := r.Header.Get("x-izkp-challenge") if chalString == "" { glog.Infof("didn't receive a challenge, so using a raw hash") d := make([]byte, 64) sha3.ShakeSum256(d, b) w.Write(d) return } challenge := []byte(chalString) glog.Infof("received a challenge of length %d", len(challenge)) h := sha3.New512() h.Write(challenge) h.Write(b) d := make([]byte, 64) h.Sum(d) w.Write(d) return } }
// Checksum returns the checksum of some data, using a specified algorithm. // It only returns an error when an invalid algorithm is used. The valid ones // are MD5, SHA1, SHA224, SHA256, SHA384, SHA512, SHA3224, SHA3256, SHA3384, // and SHA3512. func Checksum(algorithm string, data []byte) (checksum string, err error) { // default var hasher hash.Hash switch strings.ToUpper(algorithm) { case "MD5": hasher = md5.New() case "SHA1": hasher = sha1.New() case "SHA224": hasher = sha256.New224() case "SHA256": hasher = sha256.New() case "SHA384": hasher = sha512.New384() case "SHA512": hasher = sha512.New() case "SHA3224": hasher = sha3.New224() case "SHA3256": hasher = sha3.New256() case "SHA3384": hasher = sha3.New384() case "SHA3512": hasher = sha3.New512() default: msg := "Invalid algorithm parameter passed go Checksum: %s" return checksum, fmt.Errorf(msg, algorithm) } hasher.Write(data) str := hex.EncodeToString(hasher.Sum(nil)) return str, nil }
func sumSHA3(data []byte) ([]byte, error) { h := sha3.New512() if _, err := h.Write(data); err != nil { return nil, err } return h.Sum(nil), nil }
func computeHash(msg []byte, bitsize int) ([]byte, error) { var hash hash.Hash switch bitsize { case 224: hash = sha3.New224() case 256: hash = sha3.New256() case 384: hash = sha3.New384() case 512: hash = sha3.New512() case 521: hash = sha3.New512() default: return nil, fmt.Errorf("Invalid bitsize. It was [%d]. Expected [224, 256, 384, 512, 521]", bitsize) } hash.Write(msg) return hash.Sum(nil), nil }
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 }
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) } } }
for i := 0; i < n; i++ { if !ecdsa.Verify(&priv.PublicKey, hashed, r, s) { fmt.Printf("Fail\n") os.Exit(1) } } return int(float64(n) / time.Since(start).Seconds()), "operations" } var shaBuf = make([]byte, 8192) var sha256Object = sha256.New() var sha512Object = sha512.New() var sha3_256Object = sha3.New256() var sha3_512Object = sha3.New512() func sha256Bench(size int, n int) int { sum := make([]byte, sha256Object.Size()) start := time.Now() for i := 0; i < n; i++ { sha256Object.Reset() sha256Object.Write(shaBuf[:size]) sha256Object.Sum(sum[:0]) } return int(float64(size*n) / time.Since(start).Seconds()) }