diff options
| author | Shulhan <ms@kilabit.info> | 2023-09-22 02:16:44 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-09-22 02:16:44 +0700 |
| commit | ac811b84af2461539d6b04c3d95eed8e5438ec25 (patch) | |
| tree | 5d16b30f5059f127ccba9c0ec26a1885e3ccaa62 | |
| parent | 63287a8cd84d461b731014f42d3ef32693f6f206 (diff) | |
| download | awwan-ac811b84af2461539d6b04c3d95eed8e5438ec25.tar.xz | |
all: split the decrypt into separate function
The decrypt function accept the private key and cipher text to be
decrypted.
This will allow the function to be used by other methods later, not
only by Decrypt method.
| -rw-r--r-- | awwan.go | 31 |
1 files changed, 20 insertions, 11 deletions
@@ -140,10 +140,6 @@ func (aww *Awwan) Decrypt(fileVault string) (filePlain string, err error) { return ``, fmt.Errorf(`%s: invalid extension, expecting %s, got %s`, logp, defEncryptExt, ext) } - if aww.privateKey == nil { - return ``, fmt.Errorf(`%s: missing private key %s`, logp, defFilePrivateKey) - } - var ciphertext []byte ciphertext, err = os.ReadFile(fileVault) @@ -151,14 +147,9 @@ func (aww *Awwan) Decrypt(fileVault string) (filePlain string, err error) { return ``, fmt.Errorf(`%s: %w`, logp, err) } - var ( - hash = sha256.New() - label = []byte(`awwan`) - - plaintext []byte - ) + var plaintext []byte - plaintext, err = libcrypto.DecryptOaep(hash, rand.Reader, aww.privateKey, ciphertext, label) + plaintext, err = decrypt(aww.privateKey, ciphertext) if err != nil { return ``, fmt.Errorf(`%s: %w`, logp, err) } @@ -475,6 +466,24 @@ func (aww *Awwan) loadPrivateKey() (err error) { return nil } +func decrypt(pkey *rsa.PrivateKey, cipher []byte) (plain []byte, err error) { + if pkey == nil { + return nil, fmt.Errorf(`missing private key file %q`, defFilePrivateKey) + } + + var ( + hash = sha256.New() + label = []byte(`awwan`) + ) + + plain, err = libcrypto.DecryptOaep(hash, rand.Reader, pkey, cipher, label) + if err != nil { + return nil, err + } + + return plain, nil +} + // lookupBaseDir find the directory that contains ".ssh" directory from // current working directory until "/", as the base working directory of // awwan. |
