cmd/age: add a prototype of the command line tool

This commit is contained in:
Filippo Valsorda
2019-10-06 23:16:20 -04:00
parent 37d95cc84a
commit 0940f184fb
13 changed files with 266 additions and 11 deletions

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package age
import (

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package age_test
import (

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package age
import (

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package age_test
import (

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package age
import (

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package age
import (

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package age
import (
@@ -6,6 +12,7 @@ import (
"errors"
"fmt"
"io"
"strings"
"github.com/FiloSottile/age/internal/format"
"golang.org/x/crypto/chacha20poly1305"
@@ -32,6 +39,22 @@ func NewX25519Recipient(publicKey []byte) (*X25519Recipient, error) {
return r, nil
}
func ParseX25519Recipient(s string) (*X25519Recipient, error) {
if !strings.HasPrefix(s, "pubkey:") {
return nil, fmt.Errorf("malformed recipient: %s", s)
}
pubKey := strings.TrimPrefix(s, "pubkey:")
k, err := format.DecodeString(pubKey)
if err != nil {
return nil, fmt.Errorf("malformed recipient: %s", s)
}
r, err := NewX25519Recipient(k)
if err != nil {
return nil, fmt.Errorf("malformed recipient: %s", s)
}
return r, nil
}
func (r *X25519Recipient) Wrap(fileKey []byte) (*format.Recipient, error) {
var ephemeral, ourPublicKey [32]byte
if _, err := rand.Read(ephemeral[:]); err != nil {
@@ -65,6 +88,10 @@ func (r *X25519Recipient) Wrap(fileKey []byte) (*format.Recipient, error) {
return l, nil
}
func (r *X25519Recipient) String() string {
return "pubkey:" + format.EncodeToString(r.theirPublicKey[:])
}
type X25519Identity struct {
secretKey, ourPublicKey [32]byte
}
@@ -83,6 +110,22 @@ func NewX25519Identity(secretKey []byte) (*X25519Identity, error) {
return i, nil
}
func ParseX25519Identity(s string) (*X25519Identity, error) {
if !strings.HasPrefix(s, "AGE_SECRET_KEY_") {
return nil, fmt.Errorf("malformed secret key: %s", s)
}
privKey := strings.TrimPrefix(s, "AGE_SECRET_KEY_")
k, err := format.DecodeString(privKey)
if err != nil {
return nil, fmt.Errorf("malformed secret key: %s", s)
}
r, err := NewX25519Identity(k)
if err != nil {
return nil, fmt.Errorf("malformed secret key: %s", s)
}
return r, nil
}
func (i *X25519Identity) Unwrap(block *format.Recipient) ([]byte, error) {
if block.Type != "X25519" {
return nil, errors.New("wrong recipient block type")
@@ -121,3 +164,13 @@ func (i *X25519Identity) Unwrap(block *format.Recipient) ([]byte, error) {
}
return fileKey, nil
}
func (i *X25519Identity) Recipient() *X25519Recipient {
r := &X25519Recipient{}
r.theirPublicKey = i.ourPublicKey
return r
}
func (i *X25519Identity) String() string {
return "AGE_SECRET_KEY_" + format.EncodeToString(i.secretKey[:])
}

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package format
import (

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package stream
import (
@@ -156,8 +162,7 @@ func (w *Writer) Write(p []byte) (n int, err error) {
total := len(p)
for len(p) > 0 {
free := ChunkSize - len(w.unwritten)
freeBuf := w.buf[len(w.unwritten) : len(w.unwritten)+free]
freeBuf := w.buf[len(w.unwritten):ChunkSize]
n := copy(freeBuf, p)
p = p[n:]
w.unwritten = w.unwritten[:len(w.unwritten)+n]

View File

@@ -1,3 +1,9 @@
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package stream_test
import (