helpers.go 984 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package ethutil
  2. import (
  3. "code.google.com/p/go.crypto/ripemd160"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "github.com/obscuren/sha3"
  7. "strconv"
  8. )
  9. func Uitoa(i uint32) string {
  10. return strconv.FormatUint(uint64(i), 10)
  11. }
  12. func Sha256Bin(data []byte) []byte {
  13. hash := sha256.Sum256(data)
  14. return hash[:]
  15. }
  16. func Ripemd160(data []byte) []byte {
  17. ripemd := ripemd160.New()
  18. ripemd.Write(data)
  19. return ripemd.Sum(nil)
  20. }
  21. func Sha3Bin(data []byte) []byte {
  22. d := sha3.NewKeccak256()
  23. d.Reset()
  24. d.Write(data)
  25. return d.Sum(nil)
  26. }
  27. // Helper function for comparing slices
  28. func CompareIntSlice(a, b []int) bool {
  29. if len(a) != len(b) {
  30. return false
  31. }
  32. for i, v := range a {
  33. if v != b[i] {
  34. return false
  35. }
  36. }
  37. return true
  38. }
  39. // Returns the amount of nibbles that match each other from 0 ...
  40. func MatchingNibbleLength(a, b []int) int {
  41. i := 0
  42. for CompareIntSlice(a[:i+1], b[:i+1]) && i < len(b) {
  43. i += 1
  44. }
  45. return i
  46. }
  47. func Hex(d []byte) string {
  48. return hex.EncodeToString(d)
  49. }