-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added capability to set armored headers on a per-file basis
- Loading branch information
Lyor Goldstein
committed
Feb 13, 2020
1 parent
398474b
commit 8b2a8bb
Showing
6 changed files
with
154 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,4 @@ public String toString() { | |
+ ", numArmorHeaders=" + getArmorHeaders().size() | ||
+ "]"; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/org/c02e/jpgpj/EncryptedAsciiArmorHeadersCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.c02e.jpgpj; | ||
|
||
/** | ||
* Used by the encryptor to allow users to configure per-file | ||
* armored headers instead/in addition to the global ones that | ||
* are set by the encryptor | ||
*/ | ||
@FunctionalInterface | ||
public interface EncryptedAsciiArmorHeadersCallback { | ||
/** | ||
* Invoked by the encryptor <U>after</U> updating the | ||
* settings with the configured global headers. | ||
* | ||
* @param encryptor The {@link Encryptor} that is handling the encryption request | ||
* @param meta The input plaintext {@link FileMetadata} - might be empty | ||
* (but not {@code null}). | ||
* @param manipulator The manipulator that can be used to update the headers | ||
*/ | ||
void prepareAsciiArmoredHeaders( | ||
Encryptor encryptor, FileMetadata meta, EncryptedAsciiArmorHeadersManipulator manipulator); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/org/c02e/jpgpj/EncryptedAsciiArmorHeadersManipulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.c02e.jpgpj; | ||
|
||
import java.util.Map; | ||
|
||
import org.bouncycastle.bcpg.ArmoredOutputStream; | ||
|
||
@FunctionalInterface | ||
public interface EncryptedAsciiArmorHeadersManipulator { | ||
/** | ||
* A manipulator that ignores all headers manipulations | ||
*/ | ||
EncryptedAsciiArmorHeadersManipulator EMPTY = (name, value) -> { /* do nothing */ }; | ||
|
||
/** | ||
* Set the specified header value - replace any previous value | ||
* | ||
* @param name Case <U>sensitive</U> name of header to set. <B>Note:</B> this | ||
* method can be used to <U>override</U> the default version header value. | ||
* @param value Value to set - if {@code null} then equivalent to header removal | ||
*/ | ||
void setHeader(String name, String value); | ||
|
||
/** | ||
* Removes specified header - no-op if header not set anyway | ||
* | ||
* @param name Case <U>sensitive</U> name of header to set. <B>Note:</B> this | ||
* method can be used to <U>remove</U> the default version header value. | ||
*/ | ||
default void removeHeader(String name) { | ||
setHeader(name, null); | ||
} | ||
|
||
/** | ||
* Replaces existing headers and adds missing ones | ||
* | ||
* @param headers The headers to update - ignored if {@code null}. | ||
* <B>Note:</B> header name is case <U>sensitive</U> | ||
*/ | ||
default void updateHeaders(Map<String, String> headers) { | ||
if (headers != null) { | ||
headers.forEach((name, value) -> setHeader(name, value)); | ||
} | ||
} | ||
|
||
/** | ||
* Wraps an {@link ArmoredOutputStream} | ||
* | ||
* @param aos The stream to wrap - ignored if {@code null} | ||
* @return The manipulator wrapping | ||
*/ | ||
static EncryptedAsciiArmorHeadersManipulator wrap(ArmoredOutputStream aos) { | ||
return (aos == null) ? EMPTY : (name, value) -> aos.setHeader(name, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters