Ejemplo n.º 1
0
func (w *Put) chooseKeyID(database store.FileStore) error {
	if len(*w.keyID) != 0 {
		return nil
	}
	existingKeys, err := database.GetKeyIds()
	if err != nil && !os.IsNotExist(err) {
		return err
	}
	if len(existingKeys) > 1 {
		return errMultipleKeys
	} else if len(existingKeys) == 0 {
		return errNoKeySpecified
	}
	for keyID := range existingKeys {
		fmt.Fprintf(os.Stderr, "Using existing key %s\n", keyID)
		*w.keyID = keyID
		break
	}
	return nil
}
Ejemplo n.º 2
0
// Run runs the command.
func (w *Write) Run(database store.FileStore, provider providers.Provider) error {
	if len(*w.keyID) == 0 {
		existingKeys, err := database.GetKeyIds()
		if err != nil && !os.IsNotExist(err) {
			return err
		}
		if len(existingKeys) > 1 {
			return errMultipleKeys
		} else if len(existingKeys) == 0 {
			return errNoKeySpecified
		}
		for keyID := range existingKeys {
			fmt.Fprintf(os.Stderr, "Using existing key %s\n", keyID)
			*w.keyID = keyID
			break
		}
	}
	envelopeKey, err := provider.GenerateEnvelopeKey(*w.keyID)
	if err != nil {
		return err
	}

	box := algorithms.New(*w.algo)
	ciphertext, err := box.Encrypt(envelopeKey.GetPlaintext32(), []byte(*w.value))
	if err != nil {
		return err
	}
	encoded := base64.StdEncoding.EncodeToString(ciphertext)

	value := store.Value{
		Algorithm:     box.Label(),
		KeyID:         *w.keyID,
		KeyCiphertext: base64.StdEncoding.EncodeToString(envelopeKey.Ciphertext),
		Ciphertext:    encoded,
	}
	return database.Put(*w.name, value)
}