Skip to content

Commit

Permalink
Use password option instead of key (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne authored Nov 28, 2020
1 parent 8bb5cf1 commit e6d3359
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ brew install nakabonne/pbgopy/pbgopy
#### RHEL/CentOS

```
rpm -ivh https://github.com/nakabonne/pbgopy/releases/download/v0.1.0/pbgopy_0.1.0_linux_amd64.rpm
rpm -ivh https://github.com/nakabonne/pbgopy/releases/download/v0.1.1/pbgopy_0.1.1_linux_amd64.rpm
```

#### Debian/Ubuntu

```
wget https://github.com/nakabonne/pbgopy/releases/download/v0.1.0/pbgopy_0.1.0_linux_amd64.deb
apt install ./pbgopy_0.1.0_linux_amd64.deb
wget https://github.com/nakabonne/pbgopy/releases/download/v0.1.1/pbgopy_0.1.1_linux_amd64.deb
apt install ./pbgopy_0.1.1_linux_amd64.deb
```

#### Go
Expand All @@ -48,14 +48,14 @@ You must allow access to this port for each device you want to share data with.
pbgopy serve
```

Put the address of the host where the `pbgopy serve` process is running into `PBGOPY_SERVER` environment variable.
Populate the address of the host where the above process is running into the `PBGOPY_SERVER` environment variable. Then execute `pbgopy copy` to put the data entered in STDIN into the server.

```bash
export PBGOPY_SERVER=http://host.xz:9090
pbgopy copy <foo.png
```

Then paste it on another device:
Run `pbgopy paste` on the device you want to gain:

```bash
export PBGOPY_SERVER=http://host.xz:9090
Expand All @@ -73,13 +73,13 @@ pbgopy serve --ttl 10m
`pbgopy` comes with an ability to encrypt/decrypt with a common key, hence allows you to perform end-to-end encryption without working with external tools.

```bash
pbgopy copy -k 32-byte-or-less-string <secret.txt
pbgopy copy -p 32-byte-or-less-string <secret.txt
```

Then decrypt with the same key:
Then decrypt with the same password:

```bash
pbgopy paste -k 32-byte-or-less-string
pbgopy paste -p 32-byte-or-less-string
```

## Inspired By
Expand Down
24 changes: 12 additions & 12 deletions commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
"github.com/spf13/cobra"
)

const dummyByteForKey = byte('-')
const dummyChar = byte('-')

type copyRunner struct {
timeout time.Duration
key string
timeout time.Duration
password string

stdout io.Writer
stderr io.Writer
Expand All @@ -37,7 +37,7 @@ func NewCopyCommand(stdout, stderr io.Writer) *cobra.Command {
RunE: r.run,
}
cmd.Flags().DurationVar(&r.timeout, "timeout", 5*time.Second, "Time limit for requests")
cmd.Flags().StringVarP(&r.key, "key", "k", "", "Common key for encryption/decryption")
cmd.Flags().StringVarP(&r.password, "password", "p", "", "Password for encryption/decryption")
return cmd
}

Expand All @@ -50,8 +50,8 @@ func (r *copyRunner) run(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("failed to read from STDIN: %w", err)
}
if r.key != "" {
data, err = encrypt(r.key, data)
if r.password != "" {
data, err = encrypt(r.password, data)
if err != nil {
return fmt.Errorf("failed to encrypt the data: %w", err)
}
Expand All @@ -70,21 +70,21 @@ func (r *copyRunner) run(_ *cobra.Command, _ []string) error {
return nil
}

func encrypt(key string, data []byte) ([]byte, error) {
k := []byte(key)
length := len(k)
func encrypt(password string, data []byte) ([]byte, error) {
p := []byte(password)
length := len(p)
if length > 32 {
return nil, fmt.Errorf("the key size should be less than 32 bytes")
return nil, fmt.Errorf("the password size should be less than 32 bytes")
}
if length < 32 {
// Fill it up with dummies
n := 32 - length
for i := 0; i < n; i++ {
k = append(k, dummyByteForKey)
p = append(p, dummyChar)
}
}

block, err := aes.NewCipher(k)
block, err := aes.NewCipher(p)
if err != nil {
return nil, err
}
Expand Down
22 changes: 11 additions & 11 deletions commands/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

type pasteRunner struct {
timeout time.Duration
key string
timeout time.Duration
password string

stdout io.Writer
stderr io.Writer
Expand All @@ -34,7 +34,7 @@ func NewPasteCommand(stdout, stderr io.Writer) *cobra.Command {
RunE: r.run,
}
cmd.Flags().DurationVar(&r.timeout, "timeout", 5*time.Second, "Time limit for requests")
cmd.Flags().StringVarP(&r.key, "key", "k", "", "Common key for encryption/decryption")
cmd.Flags().StringVarP(&r.password, "password", "p", "", "Password for encryption/decryption")
return cmd
}

Expand All @@ -56,8 +56,8 @@ func (r *pasteRunner) run(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("failed to read the response body: %w", err)
}
if r.key != "" {
data, err = decrypt(r.key, data)
if r.password != "" {
data, err = decrypt(r.password, data)
if err != nil {
return fmt.Errorf("failed to decrypt the data: %w", err)
}
Expand All @@ -67,21 +67,21 @@ func (r *pasteRunner) run(_ *cobra.Command, _ []string) error {
return nil
}

func decrypt(key string, encryptedData []byte) ([]byte, error) {
k := []byte(key)
length := len(k)
func decrypt(password string, encryptedData []byte) ([]byte, error) {
p := []byte(password)
length := len(p)
if length > 32 {
return nil, fmt.Errorf("the key size should be less than 32 bytes")
return nil, fmt.Errorf("the password size should be less than 32 bytes")
}
if length < 32 {
// Fill it up with dummies
n := 32 - length
for i := 0; i < n; i++ {
k = append(k, dummyByteForKey)
p = append(p, dummyChar)
}
}

block, err := aes.NewCipher(k)
block, err := aes.NewCipher(p)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e6d3359

Please sign in to comment.