diff --git a/v2_keys.go b/v2_keys.go index 40e5e6e..6ace069 100644 --- a/v2_keys.go +++ b/v2_keys.go @@ -22,6 +22,11 @@ func NewV2AsymmetricPublicKeyFromHex(hexEncoded string) (V2AsymmetricPublicKey, return NewV2AsymmetricSecretKey().Public(), err } + return NewV2AsymmetricPublicKeyFromBytes(publicKey) +} + +// NewV2AsymmetricPublicKeyFromBytes Construct a v2 public key from bytes +func NewV2AsymmetricPublicKeyFromBytes(publicKey []byte) (V2AsymmetricPublicKey, error) { if len(publicKey) != 32 { // even though we return error, return a random key here rather than // a nil key @@ -33,7 +38,12 @@ func NewV2AsymmetricPublicKeyFromHex(hexEncoded string) (V2AsymmetricPublicKey, // ExportHex export a V2AsymmetricPublicKey to hex for storage func (k V2AsymmetricPublicKey) ExportHex() string { - return hex.EncodeToString(k.material) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes export a V2AsymmetricPublicKey to raw byte array +func (k V2AsymmetricPublicKey) ExportBytes() []byte { + return k.material } // V2AsymmetricSecretKey V2 public private key @@ -54,7 +64,12 @@ func (k V2AsymmetricSecretKey) Public() V2AsymmetricPublicKey { // ExportHex export a V2AsymmetricSecretKey to hex for storage func (k V2AsymmetricSecretKey) ExportHex() string { - return hex.EncodeToString(k.material) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes export a V2AsymmetricSecretKey to raw byte array +func (k V2AsymmetricSecretKey) ExportBytes() []byte { + return k.material } // ExportSeedHex export a V2AsymmetricSecretKey's seed to hex for storage @@ -85,6 +100,11 @@ func NewV2AsymmetricSecretKeyFromHex(hexEncoded string) (V2AsymmetricSecretKey, return NewV2AsymmetricSecretKey(), err } + return NewV2AsymmetricSecretKeyFromBytes(privateKey) +} + +// NewV2AsymmetricSecretKeyFromBytes creates a secret key from bytes +func NewV2AsymmetricSecretKeyFromBytes(privateKey []byte) (V2AsymmetricSecretKey, error) { if len(privateKey) != 64 { // even though we return error, return a random key here rather than // a nil key @@ -128,7 +148,12 @@ func NewV2SymmetricKey() V2SymmetricKey { // ExportHex exports the key as hex for storage func (k V2SymmetricKey) ExportHex() string { - return hex.EncodeToString(k.material[:]) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes exports the key as raw bytes +func (k V2SymmetricKey) ExportBytes() []byte { + return k.material[:] } // V2SymmetricKeyFromHex constructs a key from hex @@ -140,6 +165,11 @@ func V2SymmetricKeyFromHex(hexEncoded string) (V2SymmetricKey, error) { return NewV2SymmetricKey(), err } + return V2SymmetricKeyFromBytes(bytes) +} + +// V2SymmetricKeyFromBytes constructs a key from bytes +func V2SymmetricKeyFromBytes(bytes []byte) (V2SymmetricKey, error) { if len(bytes) != 32 { // even though we return error, return a random key here rather than // a nil key diff --git a/v3_keys.go b/v3_keys.go index 9b1fef4..33a20ea 100644 --- a/v3_keys.go +++ b/v3_keys.go @@ -29,6 +29,11 @@ func NewV3AsymmetricPublicKeyFromHex(hexEncoded string) (V3AsymmetricPublicKey, return NewV3AsymmetricSecretKey().Public(), err } + return NewV3AsymmetricPublicKeyFromBytes(publicKeyBytes) +} + +// NewV3AsymmetricPublicKeyFromBytes Construct a v3 public key from bytes +func NewV3AsymmetricPublicKeyFromBytes(publicKeyBytes []byte) (V3AsymmetricPublicKey, error) { if len(publicKeyBytes) != 49 { // even though we return error, return a random key here rather than // a nil key @@ -48,7 +53,12 @@ func (k V3AsymmetricPublicKey) compressed() []byte { // ExportHex export a V3AsymmetricPublicKey to hex for storage func (k V3AsymmetricPublicKey) ExportHex() string { - return hex.EncodeToString(k.compressed()) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes export a V3AsymmetricPublicKey to raw byte array +func (k V3AsymmetricPublicKey) ExportBytes() []byte { + return k.compressed() } // V3AsymmetricSecretKey v3 public private key @@ -63,7 +73,12 @@ func (k V3AsymmetricSecretKey) Public() V3AsymmetricPublicKey { // ExportHex export a V3AsymmetricSecretKey to hex for storage func (k V3AsymmetricSecretKey) ExportHex() string { - return hex.EncodeToString(k.material.D.Bytes()) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes export a V3AsymmetricSecretKey to raw byte array +func (k V3AsymmetricSecretKey) ExportBytes() []byte { + return k.material.D.Bytes() } // NewV3AsymmetricSecretKey generate a new secret key for use with asymmetric @@ -89,6 +104,11 @@ func NewV3AsymmetricSecretKeyFromHex(hexEncoded string) (V3AsymmetricSecretKey, return NewV3AsymmetricSecretKey(), err } + return NewV3AsymmetricSecretKeyFromBytes(secretBytes) +} + +// NewV3AsymmetricSecretKeyFromBytes creates a secret key from bytes +func NewV3AsymmetricSecretKeyFromBytes(secretBytes []byte) (V3AsymmetricSecretKey, error) { if len(secretBytes) != 48 { // even though we return error, return a random key here rather than // a nil key @@ -122,7 +142,12 @@ func NewV3SymmetricKey() V3SymmetricKey { // ExportHex exports the key as hex for storage func (k V3SymmetricKey) ExportHex() string { - return hex.EncodeToString(k.material[:]) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes exports the key as raw byte array +func (k V3SymmetricKey) ExportBytes() []byte { + return k.material[:] } // V3SymmetricKeyFromHex constructs a key from hex @@ -134,6 +159,11 @@ func V3SymmetricKeyFromHex(hexEncoded string) (V3SymmetricKey, error) { return NewV3SymmetricKey(), err } + return V3SymmetricKeyFromBytes(bytes) +} + +// V3SymmetricKeyFromBytes constructs a key from bytes +func V3SymmetricKeyFromBytes(bytes []byte) (V3SymmetricKey, error) { if len(bytes) != 32 { // even though we return error, return a random key here rather than // a nil key diff --git a/v4_keys.go b/v4_keys.go index 9d05f0a..edd3487 100644 --- a/v4_keys.go +++ b/v4_keys.go @@ -24,6 +24,11 @@ func NewV4AsymmetricPublicKeyFromHex(hexEncoded string) (V4AsymmetricPublicKey, return NewV4AsymmetricSecretKey().Public(), err } + return NewV4AsymmetricPublicKeyFromBytes(publicKey) +} + +// NewV4AsymmetricPublicKeyFromBytes Construct a v4 public key from bytes +func NewV4AsymmetricPublicKeyFromBytes(publicKey []byte) (V4AsymmetricPublicKey, error) { if len(publicKey) != 32 { // even though we return error, return a random key here rather than // a nil key @@ -35,7 +40,12 @@ func NewV4AsymmetricPublicKeyFromHex(hexEncoded string) (V4AsymmetricPublicKey, // ExportHex export a V4AsymmetricPublicKey to hex for storage func (k V4AsymmetricPublicKey) ExportHex() string { - return hex.EncodeToString(k.material) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes export a V4AsymmetricPublicKey to raw byte array +func (k V4AsymmetricPublicKey) ExportBytes() []byte { + return k.material } // V4AsymmetricSecretKey v4 public private key @@ -56,7 +66,12 @@ func (k V4AsymmetricSecretKey) Public() V4AsymmetricPublicKey { // ExportHex export a V4AsymmetricSecretKey to hex for storage func (k V4AsymmetricSecretKey) ExportHex() string { - return hex.EncodeToString(k.material) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes export a V4AsymmetricSecretKey to raw byte array +func (k V4AsymmetricSecretKey) ExportBytes() []byte { + return k.material } // ExportSeedHex export a V4AsymmetricSecretKey's seed to hex for storage @@ -87,6 +102,11 @@ func NewV4AsymmetricSecretKeyFromHex(hexEncoded string) (V4AsymmetricSecretKey, return NewV4AsymmetricSecretKey(), err } + return NewV4AsymmetricSecretKeyFromBytes(privateKey) +} + +// NewV4AsymmetricSecretKeyFromBytes creates a secret key from bytes +func NewV4AsymmetricSecretKeyFromBytes(privateKey []byte) (V4AsymmetricSecretKey, error) { if len(privateKey) != 64 { // even though we return error, return a random key here rather than // a nil key @@ -130,7 +150,12 @@ func NewV4SymmetricKey() V4SymmetricKey { // ExportHex exports the key as hex for storage func (k V4SymmetricKey) ExportHex() string { - return hex.EncodeToString(k.material[:]) + return hex.EncodeToString(k.ExportBytes()) +} + +// ExportBytes exports the key as raw byte array +func (k V4SymmetricKey) ExportBytes() []byte { + return k.material[:] } // V4SymmetricKeyFromHex constructs a key from hex @@ -143,6 +168,11 @@ func V4SymmetricKeyFromHex(hexEncoded string) (V4SymmetricKey, error) { return NewV4SymmetricKey(), err } + return V4SymmetricKeyFromBytes(bytes) +} + +// V4SymmetricKeyFromBytes constructs a key from bytes +func V4SymmetricKeyFromBytes(bytes []byte) (V4SymmetricKey, error) { if len(bytes) != 32 { // even though we return error, return a random key here rather than // a nil key