Skip to content

Commit

Permalink
refactoring code and updating dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed Jun 4, 2024
1 parent 06719cd commit edebc48
Show file tree
Hide file tree
Showing 23 changed files with 183 additions and 177 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ import (
)

func main() {
// Creating a new private key for password
privKey, err := xipher.NewPrivateKeyForPassword([]byte("Paws0meKittyKuwan!"))
// Creating a new secret key for password
scrtKey, err := xipher.NewSecretKeyForPassword([]byte("Paws0meKittyKuwan!"))
if err != nil {
panic(err)
}

// Deriving public key from private key
pubKey, err := privKey.PublicKey(false)
// Deriving public key from secret key
pubKey, err := scrtKey.PublicKey(false)
if err != nil {
panic(err)
}
Expand All @@ -107,8 +107,8 @@ func main() {
}
fmt.Println("Encrypted:", base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(cipherText))

// Decrypting cipher text with private key
plainText, err := privKey.Decrypt(cipherText)
// Decrypting cipher text with secret key
plainText, err := scrtKey.Decrypt(cipherText)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion app/internal/commands/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func getVisibleInput(prompt string) (string, error) {
}

func getHiddenInputFromUser(prompt string) ([]byte, error) {
fmt.Print(prompt + "(hidden)")
fmt.Print("[Hidden] " + prompt)
input, err := term.ReadPassword(int(syscall.Stdin))
fmt.Println()
return input, err
Expand Down
2 changes: 1 addition & 1 deletion app/internal/commands/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func keygenCommand() *cobra.Command {
}
fmt.Println("Secret Key:", color.HiBlackString(secret))
} else {
password, err := getPasswordFromUser(false, ignoreFlag)
password, err := getPasswordFromUser(true, ignoreFlag)
if err != nil {
exitOnError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion app/internal/utils/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func ctFromStr(ctStr string) ([]byte, error) {
return decode(ctStr[len(xipherTxtPrefix):])
}

func DecryptTextWithSecretKey(secretKey *xipher.PrivateKey, ctStr string) (string, error) {
func DecryptTextWithSecretKey(secretKey *xipher.SecretKey, ctStr string) (string, error) {
ct, err := ctFromStr(ctStr)
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion app/internal/utils/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func NewSecretKey() (sk string, err error) {
privKey, err := xipher.NewPrivateKey()
privKey, err := xipher.NewSecretKey()
if err != nil {
return "", err
}
Expand Down
10 changes: 5 additions & 5 deletions app/internal/utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func pubKeyToStr(pubKey *xipher.PublicKey) (string, error) {
return xipherPublicKeyPrefix + encode(pubKeyBytes), nil
}

func secretKeyToStr(secretKey *xipher.PrivateKey) (string, error) {
func secretKeyToStr(secretKey *xipher.SecretKey) (string, error) {
secretKeyBytes, err := secretKey.Bytes()
if err != nil {
return "", err
Expand All @@ -33,21 +33,21 @@ func PubKeyFromStr(pubKeyStr string) (*xipher.PublicKey, error) {
return xipher.ParsePublicKey(keyBytes)
}

func secretKeyFromStr(secretKeyStr string) (*xipher.PrivateKey, error) {
func secretKeyFromStr(secretKeyStr string) (*xipher.SecretKey, error) {
if !regexp.MustCompile(secretKeyStrRegex).MatchString(secretKeyStr) {
return nil, errInvalidXipherPrivKey
}
keyBytes, err := decode(secretKeyStr[len(xipherSecretKeyPrefix):])
if err != nil {
return nil, err
}
return xipher.ParsePrivateKey(keyBytes)
return xipher.ParseSecretKey(keyBytes)
}

func SecretKeyFromSecret(secret string) (*xipher.PrivateKey, error) {
func SecretKeyFromSecret(secret string) (*xipher.SecretKey, error) {
secretKey, err := secretKeyFromStr(secret)
if err != nil {
secretKey, err = xipher.NewPrivateKeyForPassword([]byte(secret))
secretKey, err = xipher.NewSecretKeyForPassword([]byte(secret))
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ require (
github.com/cloudflare/circl v1.3.8
github.com/fatih/color v1.17.0
github.com/spf13/cobra v1.8.0
golang.org/x/crypto v0.23.0
golang.org/x/term v0.20.0
golang.org/x/crypto v0.24.0
golang.org/x/term v0.21.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/sys v0.21.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 3 additions & 3 deletions internal/asx/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const (
// PrivateKeyLength is the allowed length of the private key
PrivateKeyLength = kyb.PrivateKeyLength
// MinPublicKeyLength is the minimum length allowed for the public key
MinPublicKeyLength = ecc.KeyLength + 1
MinPublicKeyLength = ecc.KeyLength + 1 // +1 for the algorithm type

// Algorithm Types
AlgoECC uint8 = 0
AlgoKyber uint8 = 1
algoECC uint8 = 0
algoKyber uint8 = 1
)

var (
Expand Down
14 changes: 7 additions & 7 deletions internal/asx/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
// NewEncryptingWriter returns a new WriteCloser that encrypts data with the public key and writes to dst.
func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool) (io.WriteCloser, error) {
if publicKey.ePub != nil {
if _, err := dst.Write([]byte{AlgoECC}); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write algorithm: %w", "xipher", err)
if _, err := dst.Write([]byte{algoECC}); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write algorithm", "xipher")
}
return publicKey.ePub.NewEncryptingWriter(dst, compression)
} else if publicKey.kPub != nil {
if _, err := dst.Write([]byte{AlgoKyber}); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write algorithm: %w", "xipher", err)
if _, err := dst.Write([]byte{algoKyber}); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write algorithm", "xipher")
}
return publicKey.kPub.NewEncryptingWriter(dst, compression)
} else {
Expand All @@ -26,16 +26,16 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
algoBytes := make([]byte, 1)
if _, err := io.ReadFull(src, algoBytes); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read algorithm: %w", "xipher", err)
return nil, fmt.Errorf("%s: decrypter failed to read algorithm", "xipher")
}
var algo uint8 = algoBytes[0]
if algo == AlgoECC {
if algo == algoECC {
eccPrivKey, err := privateKey.getEccPrivKey()
if err != nil {
return nil, err
}
return eccPrivKey.NewDecryptingReader(src)
} else if algo == AlgoKyber {
} else if algo == algoKyber {
kybPrivKey, err := privateKey.getKybPrivKey()
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions internal/asx/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (privateKey *PrivateKey) Bytes() []byte {
func NewPrivateKey() (*PrivateKey, error) {
key := make([]byte, PrivateKeyLength)
if _, err := rand.Read(key); err != nil {
return nil, fmt.Errorf("%s: failed to generate private key: %w", "xipher", err)
return nil, fmt.Errorf("%s: failed to generate private key", "xipher")
}
return ParsePrivateKey(key)
}
Expand Down Expand Up @@ -111,13 +111,13 @@ func (privateKey *PrivateKey) PublicKeyKyber() (*PublicKey, error) {
// Bytes returns the public key as bytes.
func (publicKey *PublicKey) Bytes() ([]byte, error) {
if publicKey.ePub != nil {
return append([]byte{AlgoECC}, publicKey.ePub.Bytes()...), nil
return append([]byte{algoECC}, publicKey.ePub.Bytes()...), nil
} else if publicKey.kPub != nil {
kybPubKeyBytes, err := publicKey.kPub.Bytes()
if err != nil {
return nil, err
}
return append([]byte{AlgoKyber}, kybPubKeyBytes...), nil
return append([]byte{algoKyber}, kybPubKeyBytes...), nil
} else {
return nil, errInvalidPublicKey
}
Expand All @@ -129,15 +129,15 @@ func ParsePublicKey(key []byte) (*PublicKey, error) {
return nil, errInvalidPublicKeyLength
}
switch key[0] {
case AlgoECC:
case algoECC:
eccPubKey, err := ecc.ParsePublicKey(key[1:])
if err != nil {
return nil, err
}
return &PublicKey{
ePub: eccPubKey,
}, nil
case AlgoKyber:
case algoKyber:
kybPubKey, err := kyb.ParsePublicKey(key[1:])
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions internal/ecc/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
return nil, err
}
if _, err = dst.Write(encrypter.ephPubKey); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write ephemeral public key: %w", "xipher", err)
return nil, fmt.Errorf("%s: encrypter failed to write ephemeral public key", "xipher")
}
return (*encrypter.cipher).NewEncryptingWriter(dst, compression)
}
Expand All @@ -24,11 +24,11 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
ephPubKey := make([]byte, KeyLength)
if _, err := io.ReadFull(src, ephPubKey); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read ephemeral public key: %w", "xipher", err)
return nil, fmt.Errorf("%s: decrypter failed to read ephemeral public key", "xipher")
}
sharedKey, err := curve25519.X25519(*privateKey.key, ephPubKey)
if err != nil {
return nil, fmt.Errorf("%s: decrypter failed to generate shared key: %w", "xipher", err)
return nil, fmt.Errorf("%s: decrypter failed to generate shared key", "xipher")
}
decrypter, err := xcp.New(sharedKey)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/ecc/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (privateKey *PrivateKey) Bytes() []byte {
func NewPrivateKey() (*PrivateKey, error) {
key := make([]byte, KeyLength)
if _, err := rand.Read(key); err != nil {
return nil, fmt.Errorf("%s: failed to generate private key: %w", "xipher", err)
return nil, fmt.Errorf("%s: failed to generate private key", "xipher")
}
return ParsePrivateKey(key)
}
Expand All @@ -59,7 +59,7 @@ func (privateKey *PrivateKey) PublicKey() (*PublicKey, error) {
if privateKey.publicKey == nil {
key, err := curve25519.X25519(*privateKey.key, curve25519.Basepoint)
if err != nil {
return nil, fmt.Errorf("%s: failed to generate public key: %w", "xipher", err)
return nil, fmt.Errorf("%s: failed to generate public key", "xipher")
}
privateKey.publicKey = &PublicKey{
key: &key,
Expand Down Expand Up @@ -87,15 +87,15 @@ func (publicKey *PublicKey) getEncrypter() (*encrypter, error) {
if publicKey.encrypter == nil {
ephPrivKey := make([]byte, KeyLength)
if _, err := rand.Read(ephPrivKey); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to generate ephemeral private key: %w", "xipher", err)
return nil, fmt.Errorf("%s: encrypter failed to generate ephemeral private key", "xipher")
}
ephPubKey, err := curve25519.X25519(ephPrivKey, curve25519.Basepoint)
if err != nil {
return nil, fmt.Errorf("%s: encrypter failed to generate ephemeral public key: %w", "xipher", err)
return nil, fmt.Errorf("%s: encrypter failed to generate ephemeral public key", "xipher")
}
sharedKey, err := curve25519.X25519(ephPrivKey, *publicKey.key)
if err != nil {
return nil, fmt.Errorf("%s: encrypter failed to generate shared key: %w", "xipher", err)
return nil, fmt.Errorf("%s: encrypter failed to generate shared key", "xipher")
}
cipher, err := xcp.New(sharedKey)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/kyb/cypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
return nil, err
}
if _, err = dst.Write(encrypter.keyEnc); err != nil {
return nil, fmt.Errorf("%s: encrypter failed to write encapsulated key: %w", "xipher", err)
return nil, fmt.Errorf("%s: encrypter failed to write encapsulated key", "xipher")
}
return (*encrypter.cipher).NewEncryptingWriter(dst, compression)
}
Expand All @@ -24,11 +24,11 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
keyEnc := make([]byte, ctLength)
if _, err := io.ReadFull(src, keyEnc); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read encapsulated key: %w", "xipher", err)
return nil, fmt.Errorf("%s: decrypter failed to read encapsulated key", "xipher")
}
sharedKey, err := kyber1024.Scheme().Decapsulate(privateKey.sk, keyEnc)
if err != nil {
return nil, fmt.Errorf("%s: decrypter failed to generate shared key: %w", "xipher", err)
return nil, fmt.Errorf("%s: decrypter failed to generate shared key", "xipher")
}
decrypter, err := xcp.New(sharedKey)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/kyb/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (privateKey *PrivateKey) Bytes() []byte {
func NewPrivateKey() (*PrivateKey, error) {
key := make([]byte, PrivateKeyLength)
if _, err := rand.Read(key); err != nil {
return nil, fmt.Errorf("%s: failed to generate private key: %w", "xipher", err)
return nil, fmt.Errorf("%s: failed to generate private key", "xipher")
}
return NewPrivateKeyForSeed(key)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (publicKey *PublicKey) getEncrypter() (*encrypter, error) {
if publicKey.encrypter == nil {
keyEnc, sharedKey, err := kyber1024.Scheme().Encapsulate(publicKey.pk)
if err != nil {
return nil, fmt.Errorf("%s: failed to encapsulate shared key: %w", "xipher", err)
return nil, fmt.Errorf("%s: failed to encapsulate shared key", "xipher")
}
cipher, err := xcp.New(sharedKey)
if err != nil {
Expand Down
Loading

0 comments on commit edebc48

Please sign in to comment.