• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pgpainless / sop-java / #54

25 Sep 2025 09:07PM UTC coverage: 58.308% (-0.1%) from 58.411%
#54

push

other

vanitasvitae
Remove unused import

2088 of 3581 relevant lines covered (58.31%)

0.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.86
/sop-java/src/main/kotlin/sop/operation/Encrypt.kt
1
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
2
//
3
// SPDX-License-Identifier: Apache-2.0
4

5
package sop.operation
6

7
import java.io.IOException
8
import java.io.InputStream
9
import sop.EncryptionResult
10
import sop.Profile
11
import sop.ReadyWithResult
12
import sop.enums.EncryptAs
13
import sop.exception.SOPGPException.*
14
import sop.util.UTF8Util
15

16
/** Interface for creating encrypted OpenPGP messages. */
17
interface Encrypt {
18

19
    /**
20
     * Disable ASCII armor encoding.
21
     *
22
     * @return builder instance
23
     */
24
    fun noArmor(): Encrypt
25

26
    /**
27
     * Sets encryption mode.
28
     *
29
     * @param mode mode
30
     * @return builder instance
31
     * @throws UnsupportedOption if this option is not supported
32
     */
33
    @Throws(UnsupportedOption::class) fun mode(mode: EncryptAs): Encrypt
34

35
    /**
36
     * Adds the signer key.
37
     *
38
     * @param key input stream containing the encoded signer key
39
     * @return builder instance
40
     * @throws KeyCannotSign if the key cannot be used for signing
41
     * @throws UnsupportedAsymmetricAlgo if the key uses an unsupported asymmetric algorithm
42
     * @throws BadData if the [InputStream] does not contain an OpenPGP key
43
     * @throws IOException in case of an IO error
44
     */
45
    @Throws(
46
        KeyCannotSign::class, UnsupportedAsymmetricAlgo::class, BadData::class, IOException::class)
47
    fun signWith(key: InputStream): Encrypt
48

49
    /**
50
     * Adds the signer key.
51
     *
52
     * @param key byte array containing the encoded signer key
53
     * @return builder instance
54
     * @throws KeyCannotSign if the key cannot be used for signing
55
     * @throws UnsupportedAsymmetricAlgo if the key uses an unsupported asymmetric algorithm
56
     * @throws BadData if the byte array does not contain an OpenPGP key
57
     * @throws IOException in case of an IO error
58
     */
59
    @Throws(
60
        KeyCannotSign::class, UnsupportedAsymmetricAlgo::class, BadData::class, IOException::class)
61
    fun signWith(key: ByteArray): Encrypt = signWith(key.inputStream())
1✔
62

63
    /**
64
     * Provide the password for the secret key used for signing.
65
     *
66
     * @param password password
67
     * @return builder instance
68
     * @throws PasswordNotHumanReadable if the password is not human-readable
69
     * @throws UnsupportedOption if key password are not supported
70
     */
71
    @Throws(PasswordNotHumanReadable::class, UnsupportedOption::class)
72
    fun withKeyPassword(password: String): Encrypt =
×
73
        withKeyPassword(password.toByteArray(UTF8Util.UTF8))
×
74

75
    /**
76
     * Provide the password for the secret key used for signing.
77
     *
78
     * @param password password
79
     * @return builder instance
80
     * @throws PasswordNotHumanReadable if the password is not human-readable
81
     * @throws UnsupportedOption if key password are not supported
82
     */
83
    @Throws(PasswordNotHumanReadable::class, UnsupportedOption::class)
84
    fun withKeyPassword(password: ByteArray): Encrypt
85

86
    /**
87
     * Encrypt with the given password.
88
     *
89
     * @param password password
90
     * @return builder instance
91
     * @throws PasswordNotHumanReadable if the password is not human-readable
92
     * @throws UnsupportedOption if this option is not supported
93
     */
94
    @Throws(PasswordNotHumanReadable::class, UnsupportedOption::class)
95
    fun withPassword(password: String): Encrypt
96

97
    /**
98
     * Encrypt with the given cert.
99
     *
100
     * @param cert input stream containing the encoded cert.
101
     * @return builder instance
102
     * @throws CertCannotEncrypt if the certificate is not encryption capable
103
     * @throws UnsupportedAsymmetricAlgo if the certificate uses an unsupported asymmetric algorithm
104
     * @throws BadData if the [InputStream] does not contain an OpenPGP certificate
105
     * @throws IOException in case of an IO error
106
     */
107
    @Throws(
108
        CertCannotEncrypt::class,
109
        UnsupportedAsymmetricAlgo::class,
110
        BadData::class,
111
        IOException::class)
112
    fun withCert(cert: InputStream): Encrypt
113

114
    /**
115
     * Encrypt with the given cert.
116
     *
117
     * @param cert byte array containing the encoded cert.
118
     * @return builder instance
119
     * @throws CertCannotEncrypt if the certificate is not encryption capable
120
     * @throws UnsupportedAsymmetricAlgo if the certificate uses an unsupported asymmetric algorithm
121
     * @throws BadData if the byte array does not contain an OpenPGP certificate
122
     * @throws IOException in case of an IO error
123
     */
124
    @Throws(
125
        CertCannotEncrypt::class,
126
        UnsupportedAsymmetricAlgo::class,
127
        BadData::class,
128
        IOException::class)
129
    fun withCert(cert: ByteArray): Encrypt = withCert(cert.inputStream())
1✔
130

131
    /**
132
     * Pass in a profile.
133
     *
134
     * @param profile profile
135
     * @return builder instance
136
     */
137
    fun profile(profile: Profile): Encrypt = profile(profile.name)
×
138

139
    /**
140
     * Pass in a profile identifier.
141
     *
142
     * @param profileName profile identifier
143
     * @return builder instance
144
     */
145
    fun profile(profileName: String): Encrypt
146

147
    /**
148
     * Encrypt the given data yielding the ciphertext.
149
     *
150
     * @param plaintext plaintext
151
     * @return result and ciphertext
152
     * @throws IOException in case of an IO error
153
     * @throws KeyIsProtected if at least one signing key cannot be unlocked
154
     */
155
    @Throws(IOException::class, KeyIsProtected::class)
156
    fun plaintext(plaintext: InputStream): ReadyWithResult<EncryptionResult>
157

158
    /**
159
     * Encrypt the given data yielding the ciphertext.
160
     *
161
     * @param plaintext plaintext
162
     * @return result and ciphertext
163
     * @throws IOException in case of an IO error
164
     * @throws KeyIsProtected if at least one signing key cannot be unlocked
165
     */
166
    @Throws(IOException::class, KeyIsProtected::class)
167
    fun plaintext(plaintext: ByteArray): ReadyWithResult<EncryptionResult> =
×
168
        plaintext(plaintext.inputStream())
1✔
169
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc