Skip to content

Commit

Permalink
Merge pull request #2 from suttod/raw-import-export-keys
Browse files Browse the repository at this point in the history
Add feature to import/export keys as raw byte array
  • Loading branch information
aidantwoods authored May 30, 2022
2 parents 70af9a6 + e51fb25 commit db2f9f6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
36 changes: 33 additions & 3 deletions v2_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 33 additions & 3 deletions v3_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 33 additions & 3 deletions v4_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit db2f9f6

Please sign in to comment.