Exemple #1
0
// Run runs the command.
func (w *Put) Run(database store.FileStore) error {
	var value store.Value
	algo, err := algorithms.New(*w.algo)
	if err != nil {
		return err
	}
	value.Algorithm = algo.Label()

	var envelopeKey keymanager.EnvelopeKey
	if algo.NeedsKey() {
		keyManager, err := keymanager.New(*w.keyManager)
		if err != nil {
			return err
		}
		value.KeyManager = keyManager.Label()

		if err := w.chooseKeyID(database); err != nil {
			return err
		}
		value.KeyID = *w.keyID

		envelopeKey, err = keyManager.GenerateEnvelopeKey(*w.keyID, *w.name)
		if err != nil {
			return err
		}
		value.KeyCiphertext = base64.StdEncoding.EncodeToString(envelopeKey.Ciphertext)
	}

	plaintext, err := w.choosePlaintext()
	if err != nil {
		return err
	}

	ciphertext, err := algo.Encrypt(envelopeKey.GetPlaintext32(), plaintext)
	if err != nil {
		return err
	}
	value.Ciphertext = base64.StdEncoding.EncodeToString(ciphertext)

	return database.Put(*w.name, value)
}
Exemple #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)
}