Ejemplo n.º 1
0
// Run runs the command.
func (r *Read) Run(database store.FileStore, provider providers.Provider) error {
	value, err := database.Get(*r.name)
	if err != nil {
		return err
	}
	algo := algorithms.New(value.Algorithm)
	var decryptionKeyArray [32]byte
	if algo.NeedsKey() {
		keyCiphertext, err2 := value.GetKeyCiphertext()
		if err2 != nil {
			return err2
		}
		keyPlaintext, err2 := provider.Decrypt(keyCiphertext)
		if err2 != nil {
			return err2
		}
		copy(decryptionKeyArray[:], keyPlaintext)
	}
	decoded, err := value.GetCiphertext()
	if err != nil {
		return err
	}
	plaintext, err := algo.Decrypt(&decryptionKeyArray, decoded)
	if err != nil {
		return err
	}
	fmt.Printf("%s\n", plaintext)
	return nil
}
Ejemplo n.º 2
0
// Run runs the command.
func (r *Get) Run(database store.FileStore) error {
	value, err := database.Get(*r.name)
	if err != nil {
		return err
	}
	algo, err := algorithms.New(value.Algorithm)
	if err != nil {
		return err
	}
	var decryptionKeyArray [32]byte
	if algo.NeedsKey() {
		keyManager, err := keymanager.New(value.KeyManager)
		if err != nil {
			return err
		}

		keyCiphertext, err2 := value.GetKeyCiphertext()
		if err2 != nil {
			return err2
		}
		keyPlaintext, err2 := keyManager.Decrypt(keyCiphertext, *r.name)
		if err2 != nil {
			return err2
		}
		copy(decryptionKeyArray[:], keyPlaintext)
	}
	decoded, err := value.GetCiphertext()
	if err != nil {
		return err
	}
	plaintext, err := algo.Decrypt(&decryptionKeyArray, decoded)
	if err != nil {
		return err
	}

	if len(*r.writeTo) > 0 {
		return ioutil.WriteFile(*r.writeTo, plaintext, 0644)
	}

	fmt.Printf("%s", plaintext)
	if !bytes.HasSuffix(plaintext, []byte{'\n'}) {
		fmt.Printf("\n")
	}
	return nil
}