diff --git a/lib/common/base32/base32.go b/lib/common/base32/base32.go index d141cfe..1da6ca6 100644 --- a/lib/common/base32/base32.go +++ b/lib/common/base32/base32.go @@ -9,3 +9,10 @@ import ( // i2p base32 encoding var I2PEncoding *b32.Encoding = b32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567") + + +// wrapper arround encoding for encoding to string +func EncodeToString(data []byte) (str string) { + str = I2PEncoding.EncodeToString(data) + return +} diff --git a/lib/common/base64/base64.go b/lib/common/base64/base64.go index ad2c02d..e626fd1 100644 --- a/lib/common/base64/base64.go +++ b/lib/common/base64/base64.go @@ -9,3 +9,9 @@ import ( // i2p base64 encoding var I2PEncoding *b64.Encoding = b64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~") + +// wrapper arround encoding for encoding to string +func EncodeToString(data []byte) (str string) { + str = I2PEncoding.EncodeToString(data) + return +} diff --git a/lib/crypto/hash.go b/lib/crypto/hash.go new file mode 100644 index 0000000..457454b --- /dev/null +++ b/lib/crypto/hash.go @@ -0,0 +1,7 @@ +package crypto + +import ( + "crypto/sha256" +) + +var SHA256 = sha256.Sum256 diff --git a/lib/stdi2p/destination.go b/lib/stdi2p/destination.go index 9662698..d3e6e88 100644 --- a/lib/stdi2p/destination.go +++ b/lib/stdi2p/destination.go @@ -1,7 +1,10 @@ package stdi2p -import ( +import( + "github.com/bounce-chat/go-i2p/lib/common/base32" + "github.com/bounce-chat/go-i2p/lib/common/base64" "github.com/bounce-chat/go-i2p/lib/crypto" + "strings" ) // a network endpoint inside i2p @@ -12,6 +15,7 @@ type Destination []byte func (dest Destination) PublicKey() (k crypto.PublicEncryptionKey) { cert := dest.Certificate() if cert.Type() == CERT_KEY { + // TODO(psi): check for key cert and included encryption key } else { var ek crypto.ElgPublicKey copy(ek[:], dest[:256]) @@ -31,6 +35,7 @@ func (dest Destination) SigningPublicKey() (k crypto.SigningPublicKey) { cert := dest.Certificate() if cert.Type() == CERT_KEY { // we have a key certificate + // extract the signing key from the key cert k = KeyCert(cert).SigningPublicKey() } else { var pk crypto.DSAPublicKey @@ -39,3 +44,16 @@ func (dest Destination) SigningPublicKey() (k crypto.SigningPublicKey) { } return } + +// return the .b32.i2p address +func (dest Destination) Base32Address() (str string) { + h := crypto.SHA256(dest) + str = strings.Trim(base32.EncodeToString(h[:]), "=") + str += ".b32.i2p" + return +} + +func (dest Destination) Base64() (str string) { + str = base64.EncodeToString(dest) + return +}