func RIPEMD160Hash(data []byte) []byte { first := sha256.Sum256(data) hasher := ripemd160.New() hasher.Write(first[:]) hash := hasher.Sum(nil) return hash[:] }
func RimpHash(in []byte, out []byte) { sha := sha256.New() sha.Write(in) rim := ripemd160.New() rim.Write(sha.Sum(nil)[:]) copy(out, rim.Sum(nil)) }
func (otp *OTP) hashCalc(algorithm string) ([8]byte, error) { var hash_algorithm hash.Hash tmpseq := STAITC_OTP_OTP_REP_COUNT - (otp.seq % STAITC_OTP_OTP_REP_COUNT) _string_ := strconv.Itoa(otp.seed) + otp.passphrase switch otp.mAlgorithm { case "MD4": hash_algorithm = md4.New() case "MD5": hash_algorithm = md5.New() case "RIPEMD128": hash_algorithm = ripemd128.New() case "RIPEMD160": hash_algorithm = ripemd160.New() case "SHA1": hash_algorithm = sha1.New() default: return [8]byte{0, 0, 0, 0, 0, 0, 0, 0}, fmt.Errorf("NoSuchAlgorithmException: %s", otp.mAlgorithm) } hash_algorithm.Reset() hash_algorithm.Write(UCS2_to_UTF8([]byte(_string_))) otp.hash = hashValueTo8(hash_algorithm.Sum(nil)) for tmpseq > 0 { hash_algorithm.Reset() hash_algorithm.Write(otp.hash[:]) otp.hash = hashValueTo8(hash_algorithm.Sum(nil)) tmpseq-- } return otp.hash, nil }
// Hash160 computes RIPEMD-160(SHA-256(data)) func Hash160(data []byte) []byte { sha2 := sha256.New() sha2.Write(data) ripemd := ripemd160.New() ripemd.Write(sha2.Sum(nil)) return ripemd.Sum(nil) }
func (e *Engine) ripemd160() error { data, err := computeHash(ripemd160.New(), e.stack.Pop()) if err == nil { e.stack.Push(data) } return err }
//GetAddress returns bitcoin address from PublicKey func (pub *PublicKey) GetAddress() (string, []byte) { var publicKeyPrefix byte if pub.isTestnet { publicKeyPrefix = 0x6F } else { publicKeyPrefix = 0x00 } //Next we get a sha256 hash of the public key generated //via ECDSA, and then get a ripemd160 hash of the sha256 hash. var shadPublicKeyBytes [32]byte if pub.isCompressed { shadPublicKeyBytes = sha256.Sum256(pub.key.SerializeCompressed()) } else { shadPublicKeyBytes = sha256.Sum256(pub.key.SerializeUncompressed()) } ripeHash := ripemd160.New() ripeHash.Write(shadPublicKeyBytes[:]) ripeHashedBytes := ripeHash.Sum(nil) publicKeyEncoded := base58check.Encode(publicKeyPrefix, ripeHashedBytes) return publicKeyEncoded, ripeHashedBytes }
func Sha256RipeMD160(b []byte) []byte { ripe := ripemd160.New() sha := sha256.New() sha.Write(b) ripe.Write(sha.Sum(nil)) return ripe.Sum(nil) }
func GenerateSin(pubkey []byte) string { // FIRST STEP - COMPLETE sha_256 := sha256.New() sha_256.Write(pubkey) // SECOND STEP - COMPLETE rip := ripemd160.New() rip.Write(sha_256.Sum(nil)) // THIRD STEP - COMPLETE sin_version, _ := hex.DecodeString("0f02") pubPrefixed := append(sin_version, rip.Sum(nil)...) // FOURTH STEP - COMPLETE sha2nd := sha256.New() sha2nd.Write([]byte(pubPrefixed)) sha3rd := sha256.New() sha3rd.Write([]byte(sha2nd.Sum(nil))) // // FIFTH STEP - COMPLETE checksum := sha3rd.Sum(nil)[0:4] // SIXTH STEP - COMPLETE pubWithChecksum := append(pubPrefixed, checksum...) // SIN sin := base58.Encode(pubWithChecksum) return sin }
// ripemd160 func hash20(input []byte) (res *[20]byte) { hasher := ripemd160.New() hasher.Write(input) // does not error resSlice := hasher.Sum(nil) res = new([20]byte) copy(res[:], resSlice) return }
// NOTE: The default hash function is Ripemd160. func BinaryRipemd160(o interface{}) []byte { hasher, n, err := ripemd160.New(), new(int), new(error) WriteBinary(o, hasher, n, err) if *err != nil { PanicSanity(*err) } return hasher.Sum(nil) }
func ripemd160ofHexString(hexa string) string { byta, _ := hex.DecodeString(hexa) hash := ripemd160.New() hash.Write(byta) hashsum := hash.Sum(nil) hexb := hex.EncodeToString(hashsum) return hexb }
// General Convenience func SimpleHashFromBinary(item interface{}) []byte { hasher, n, err := ripemd160.New(), new(int), new(error) wire.WriteBinary(item, hasher, n, err) if *err != nil { PanicCrisis(err) } return hasher.Sum(nil) }
//Extended keys can be identified by the Hash160 (RIPEMD160 after SHA256) //of the serialized ECSDA public key K, ignoring the chain code //The first 32 bits of the identifier are called the key fingerprint. func (pek *ExtendedPublicKey) GetFingerPrint() []byte { sha_256 := sha256.New() sha_256.Write(pek.PublicKey) sha := sha_256.Sum(nil) ripemd := ripemd160.New() ripemd.Write(sha) final := ripemd.Sum(nil) return final[:4] }
func (part *Part) Hash() []byte { if part.hash != nil { return part.hash } else { hasher := ripemd160.New() hasher.Write(part.Bytes) // doesn't err part.hash = hasher.Sum(nil) return part.hash } }
func checkUrlIsCached(url string) (string, bool) { // check if file is in cache cdir := filepath.Join(getArriccioDir(), "cache") h := ripemd160.New() h.Write([]byte(url)) hh := h.Sum(nil) fname := filepath.Join(cdir, hex.EncodeToString(hh[:])) _, err := os.Stat(fname) return fname, (err == nil) }
func SimpleHashFromTwoHashes(left []byte, right []byte) []byte { var n int var err error var hasher = ripemd160.New() wire.WriteByteSlice(left, hasher, &n, &err) wire.WriteByteSlice(right, hasher, &n, &err) if err != nil { PanicCrisis(err) } return hasher.Sum(nil) }
//Extended keys can be identified by the Hash160 (RIPEMD160 after SHA256) //of the serialized ECSDA public key K, ignoring the chain code //The first 32 bits of the identifier are called the key fingerprint. func (ek *ExtendedKey) GetFingerPrint() []byte { _, publicKey := btcec.PrivKeyFromBytes(btcec.S256(), ek.PrivateKey) sha_256 := sha256.New() sha_256.Write(publicKey.SerializeCompressed()) sha := sha_256.Sum(nil) ripemd := ripemd160.New() ripemd.Write(sha) final := ripemd.Sum(nil) return final[:4] }
// SetPoint takes a public point and generates the corresponding address // into the receiver, complete with valid checksum. func (a *A25) SetPoint(p *Point) { c := [65]byte{4} copy(c[1:], p.x[:]) copy(c[33:], p.y[:]) h := sha256.New() h.Write(c[:]) s := h.Sum([]byte{}) h = ripemd160.New() h.Write(s) h.Sum(a[1:1]) a.UpdateChecksum() }
func (pubKey PubKeyEd25519) Address() []byte { w, n, err := new(bytes.Buffer), new(int), new(error) wire.WriteBinary(pubKey[:], w, n, err) if *err != nil { PanicCrisis(*err) } // append type byte encodedPubkey := append([]byte{PubKeyTypeEd25519}, w.Bytes()...) hasher := ripemd160.New() hasher.Write(encodedPubkey) // does not error return hasher.Sum(nil) }
func (kv KVPair) Hash() []byte { hasher, n, err := ripemd160.New(), new(int), new(error) wire.WriteString(kv.Key, hasher, n, err) if kvH, ok := kv.Value.(Hashable); ok { wire.WriteByteSlice(kvH.Hash(), hasher, n, err) } else { wire.WriteBinary(kv.Value, hasher, n, err) } if *err != nil { PanicSanity(*err) } return hasher.Sum(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) } } }
func (leaf IAVLProofLeafNode) Hash() []byte { hasher := ripemd160.New() buf := new(bytes.Buffer) n, err := int(0), error(nil) wire.WriteInt8(0, buf, &n, &err) wire.WriteVarint(1, buf, &n, &err) wire.WriteByteSlice(leaf.KeyBytes, buf, &n, &err) wire.WriteByteSlice(leaf.ValueBytes, buf, &n, &err) if err != nil { PanicCrisis(Fmt("Failed to hash IAVLProofLeafNode: %v", err)) } // fmt.Printf("LeafNode hash bytes: %X\n", buf.Bytes()) hasher.Write(buf.Bytes()) return hasher.Sum(nil) }
//BitcoinPublicKeyToAddress takes a 65 byte public key found in parsing addresses //and converts it to the 20 byte form func BitcoinPublicKeyToAddress(pubKey []byte, address *block.Address) ([]byte, []byte, error) { if pubKey[0] != 0x04 { return nil, nil, errors.New("Beginning of 65 byte public key does not match expected format") } sha1 := sha256.New() sha1.Write(pubKey) hash1 := sha1.Sum(nil) ripemd := ripemd160.New() ripemd.Write(hash1) hash160 := ripemd.Sum(nil) ret := BitcoinRipeMD160ToAddress(hash160, address) address.RipeMD160 = hex.EncodeToString(hash160) address.PublicKey = hex.EncodeToString(pubKey) return ret, hash160, nil }
// NOTE: sets hashes recursively func (node *IAVLNode) hashWithCount(t *IAVLTree) ([]byte, int) { if node.hash != nil { return node.hash, 0 } hasher := ripemd160.New() buf := new(bytes.Buffer) _, hashCount, err := node.writeHashBytes(t, buf) if err != nil { PanicCrisis(err) } // fmt.Printf("Wrote IAVL hash bytes: %X\n", buf.Bytes()) hasher.Write(buf.Bytes()) node.hash = hasher.Sum(nil) // fmt.Printf("Write IAVL hash: %X\n", node.hash) return node.hash, hashCount + 1 }
func (branch IAVLProofInnerNode) Hash(childHash []byte) []byte { hasher := ripemd160.New() buf := new(bytes.Buffer) n, err := int(0), error(nil) wire.WriteInt8(branch.Height, buf, &n, &err) wire.WriteVarint(branch.Size, buf, &n, &err) if len(branch.Left) == 0 { wire.WriteByteSlice(childHash, buf, &n, &err) wire.WriteByteSlice(branch.Right, buf, &n, &err) } else { wire.WriteByteSlice(branch.Left, buf, &n, &err) wire.WriteByteSlice(childHash, buf, &n, &err) } if err != nil { PanicCrisis(Fmt("Failed to hash IAVLProofInnerNode: %v", err)) } // fmt.Printf("InnerNode hash bytes: %X\n", buf.Bytes()) hasher.Write(buf.Bytes()) return hasher.Sum(nil) }
func calculateDigest(files []string, algorithm string) { var hasher hash.Hash switch 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 "sha512224": hasher = sha512.New512_224() case "sha512256": hasher = sha512.New512_256() case "ripemd160": hasher = ripemd160.New() default: fmt.Printf("Algorithm %s not implemented yet\n", algorithm) os.Exit(1) } for _, fn := range files { hasher.Reset() file, err := os.Open(fn) if err != nil { panic(err.Error()) } if _, err := io.Copy(hasher, file); err != nil { panic(err.Error()) } fmt.Printf("%x %s\n", hasher.Sum(nil), fn) file.Close() } }
func Ripemd160(data []byte) []byte { ripemd := ripemd160.New() ripemd.Write(data) return ripemd.Sum(nil) }
// GetHash creates hash of redeem script. func (rs *RedeemScript) getHash() []byte { shadPublicKeyBytes := sha256.Sum256(rs.Script) ripeHash := ripemd160.New() ripeHash.Write(shadPublicKeyBytes[:]) return ripeHash.Sum(nil) }
// Calculate a unique 200-bits long hash of the address func (a *StealthAddr) Hash160() []byte { rim := ripemd160.New() rim.Write(a.BytesNoPrefix()) return rim.Sum(nil) }