mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-23 11:27:57 +01:00
Add javadoc
This commit is contained in:
parent
1a8a242d86
commit
07c689b556
2 changed files with 49 additions and 2 deletions
|
@ -1,3 +1,18 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2018 Paul Schaub.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package org.pgpainless.pgpainless.key;
|
package org.pgpainless.pgpainless.key;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
|
@ -48,9 +48,22 @@ import org.pgpainless.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||||
*/
|
*/
|
||||||
public class SymmetricEncryptorDecryptor {
|
public class SymmetricEncryptorDecryptor {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypt some {@code data} symmetrically using an {@code encryptionAlgorithm} and a given {@code password}.
|
||||||
|
* The input data will be compressed using the given {@code compressionAlgorithm} and packed in a modification
|
||||||
|
* detection package, which is then encrypted.
|
||||||
|
*
|
||||||
|
* @param data bytes that will be encrypted
|
||||||
|
* @param password password that will be used to encrypt the data
|
||||||
|
* @param encryptionAlgorithm symmetric algorithm that will be used to encrypt the data
|
||||||
|
* @param compressionAlgorithm compression algorithm that will be used to compress the data
|
||||||
|
* @return encrypted data
|
||||||
|
* @throws IOException IO is dangerous
|
||||||
|
* @throws PGPException OpenPGP is brittle
|
||||||
|
*/
|
||||||
public static byte[] symmetricallyEncrypt(byte[] data,
|
public static byte[] symmetricallyEncrypt(byte[] data,
|
||||||
char[] password,
|
char[] password,
|
||||||
SymmetricKeyAlgorithm algorithm,
|
SymmetricKeyAlgorithm encryptionAlgorithm,
|
||||||
CompressionAlgorithm compressionAlgorithm)
|
CompressionAlgorithm compressionAlgorithm)
|
||||||
throws IOException, PGPException {
|
throws IOException, PGPException {
|
||||||
|
|
||||||
|
@ -59,7 +72,7 @@ public class SymmetricEncryptorDecryptor {
|
||||||
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
||||||
|
|
||||||
PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
|
PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
|
||||||
new JcePGPDataEncryptorBuilder(algorithm.getAlgorithmId())
|
new JcePGPDataEncryptorBuilder(encryptionAlgorithm.getAlgorithmId())
|
||||||
.setWithIntegrityPacket(true)
|
.setWithIntegrityPacket(true)
|
||||||
.setSecureRandom(new SecureRandom())
|
.setSecureRandom(new SecureRandom())
|
||||||
.setProvider("BC"));
|
.setProvider("BC"));
|
||||||
|
@ -74,6 +87,17 @@ public class SymmetricEncryptorDecryptor {
|
||||||
return bOut.toByteArray();
|
return bOut.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypt and decompress some symmetrically encrypted data using a password.
|
||||||
|
* Note, that decryption will fail if the given data is not integrity protected with a modification detection
|
||||||
|
* package.
|
||||||
|
*
|
||||||
|
* @param data encrypted data
|
||||||
|
* @param password password to decrypt the data
|
||||||
|
* @return decrypted data
|
||||||
|
* @throws IOException IO is dangerous
|
||||||
|
* @throws PGPException OpenPGP is brittle
|
||||||
|
*/
|
||||||
public static byte[] symmetricallyDecrypt(byte[] data, char[] password) throws IOException, PGPException {
|
public static byte[] symmetricallyDecrypt(byte[] data, char[] password) throws IOException, PGPException {
|
||||||
InputStream in = new BufferedInputStream(new ByteArrayInputStream(data));
|
InputStream in = new BufferedInputStream(new ByteArrayInputStream(data));
|
||||||
in = PGPUtil.getDecoderStream(in);
|
in = PGPUtil.getDecoderStream(in);
|
||||||
|
@ -123,6 +147,14 @@ public class SymmetricEncryptorDecryptor {
|
||||||
return outputStream.toByteArray();
|
return outputStream.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap some data in an OpenPGP compressed data package.
|
||||||
|
*
|
||||||
|
* @param clearData uncompressed data
|
||||||
|
* @param algorithm compression algorithm
|
||||||
|
* @return compressed data
|
||||||
|
* @throws IOException IO is dangerous
|
||||||
|
*/
|
||||||
private static byte[] compress(byte[] clearData, int algorithm) throws IOException
|
private static byte[] compress(byte[] clearData, int algorithm) throws IOException
|
||||||
{
|
{
|
||||||
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
||||||
|
|
Loading…
Reference in a new issue