aboutsummaryrefslogtreecommitdiffstats
path: root/crypto-common/src/main/kotlin/app/passwordstore/crypto/CryptoHandler.kt
blob: 898cf0585b0d8cbbcf1d40f70a90aea89fd8f53a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

package app.passwordstore.crypto

import app.passwordstore.crypto.errors.CryptoHandlerException
import com.github.michaelbull.result.Result
import java.io.InputStream
import java.io.OutputStream

/** Generic interface to implement cryptographic operations on top of. */
public interface CryptoHandler<Key, EncOpts : CryptoOptions, DecryptOpts : CryptoOptions> {

  /**
   * Decrypt the given [ciphertextStream] using a set of potential [keys] and [passphrase], and
   * writes the resultant plaintext to [outputStream]. The returned [Result] should be checked to
   * ensure it is **not** an instance of [com.github.michaelbull.result.Err] before the contents of
   * [outputStream] are used.
   */
  public fun decrypt(
    keys: List<Key>,
    passphrase: String,
    ciphertextStream: InputStream,
    outputStream: OutputStream,
    options: DecryptOpts,
  ): Result<Unit, CryptoHandlerException>

  /**
   * Encrypt the given [plaintextStream] to the provided [keys], and writes the encrypted ciphertext
   * to [outputStream]. The returned [Result] should be checked to ensure it is **not** an instance
   * of [com.github.michaelbull.result.Err] before the contents of [outputStream] are used.
   */
  public fun encrypt(
    keys: List<Key>,
    plaintextStream: InputStream,
    outputStream: OutputStream,
    options: EncOpts,
  ): Result<Unit, CryptoHandlerException>

  /** Given a [fileName], return whether this instance can handle it. */
  public fun canHandle(fileName: String): Boolean
}