key.go 3.4 KB
Newer Older
1 2 3
package util

import (
4 5 6
	"encoding/json"
	"fmt"

7
	b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
8
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9 10 11 12 13 14 15 16 17 18 19 20
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
)

// Key is a string representation of multihash for use with maps.
type Key string

// String is utililty function for printing out keys as strings (Pretty).
func (k Key) String() string {
	return k.Pretty()
}

// Pretty returns Key in a b58 encoded string
21
// TODO: deprecate Pretty. bad name.
22
func (k Key) Pretty() string {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	return k.B58String()
}

// B58String returns Key in a b58 encoded string
func (k Key) B58String() string {
	return B58KeyEncode(k)
}

// B58KeyDecode returns Key from a b58 encoded string
func B58KeyDecode(s string) Key {
	return Key(string(b58.Decode(s)))
}

// B58KeyEncode returns Key in a b58 encoded string
func B58KeyEncode(k Key) string {
38 39 40 41 42 43 44 45
	return b58.Encode([]byte(k))
}

// DsKey returns a Datastore key
func (k Key) DsKey() ds.Key {
	return ds.NewKey(string(k))
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// UnmarshalJSON returns a JSON-encoded Key (string)
func (k *Key) UnmarshalJSON(mk []byte) error {
	var s string
	err := json.Unmarshal(mk, &s)
	if err != nil {
		return err
	}

	*k = Key(string(b58.Decode(s)))
	if len(*k) == 0 && len(s) > 2 { // if b58.Decode fails, k == ""
		return fmt.Errorf("Key.UnmarshalJSON: invalid b58 string: %v", mk)
	}
	log.Info("Unmarshal in: %s \"%s\"", *k, s)
	return nil
}

// MarshalJSON returns a JSON-encoded Key (string)
func (k *Key) MarshalJSON() ([]byte, error) {
	b, err := json.Marshal(b58.Encode([]byte(*k)))
	log.Info("Marshal out: %s %s", *k, b)
	return b, err
}

69 70 71 72 73
// KeyFromDsKey returns a Datastore key
func KeyFromDsKey(dsk ds.Key) Key {
	return Key(dsk.BaseNamespace())
}

74 75 76 77 78 79 80
// B58KeyConverter -- for KeyTransform datastores
// (static as only one obj needed)
var B58KeyConverter = b58KeyConverter{}

type b58KeyConverter struct{}

// ConvertKey returns a B58 encoded Datastore key
81 82
// TODO: this is hacky because it encodes every path component. some
// path components may be proper strings already...
83
func (b58KeyConverter) ConvertKey(dsk ds.Key) ds.Key {
84 85
	k := ds.NewKey("/")
	for _, n := range dsk.Namespaces() {
86
		k = k.ChildString(b58.Encode([]byte(n)))
87 88 89 90
	}
	return k
}

91
// InvertKey returns a b58 decoded Datastore key
92 93
// TODO: this is hacky because it encodes every path component. some
// path components may be proper strings already...
94
func (b58KeyConverter) InvertKey(dsk ds.Key) ds.Key {
95 96
	k := ds.NewKey("/")
	for _, n := range dsk.Namespaces() {
97
		k = k.ChildString(string(b58.Decode(n)))
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	}
	return k
}

// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
func Hash(data []byte) mh.Multihash {
	h, err := mh.Sum(data, mh.SHA2_256, -1)
	if err != nil {
		// this error can be safely ignored (panic) because multihash only fails
		// from the selection of hash function. If the fn + length are valid, it
		// won't error.
		panic("multihash failed to hash using SHA2_256.")
	}
	return h
}

// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
func IsValidHash(s string) bool {
	out := b58.Decode(s)
	if out == nil || len(out) == 0 {
		return false
	}
	return true
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122 123 124 125 126 127 128 129 130

// XOR takes two byte slices, XORs them together, returns the resulting slice.
func XOR(a, b []byte) []byte {
	c := make([]byte, len(a))
	for i := 0; i < len(a); i++ {
		c[i] = a[i] ^ b[i]
	}
	return c
}