diff options
author | David Hook <dgh@cryptoworkshop.com> | 2014-06-21 08:25:48 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2014-06-21 08:25:48 +0400 |
commit | 440199f57f9b1c49a3c8b5885745886c53daa22f (patch) | |
tree | 3890e346ad003a95c24ffd48087e91a470bedbc6 /pg | |
parent | c8088daeb02f03c6a01dea9c3181c0c0b3a7a795 (diff) |
update
Diffstat (limited to 'pg')
-rw-r--r-- | pg/src/main/java/org/bouncycastle/openpgp/operator/PGPPad.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/pg/src/main/java/org/bouncycastle/openpgp/operator/PGPPad.java b/pg/src/main/java/org/bouncycastle/openpgp/operator/PGPPad.java new file mode 100644 index 00000000..c9cebe7d --- /dev/null +++ b/pg/src/main/java/org/bouncycastle/openpgp/operator/PGPPad.java @@ -0,0 +1,50 @@ +package org.bouncycastle.openpgp.operator; + +import org.bouncycastle.openpgp.PGPException; + +/** + * Utility class that provides padding addition and removal for PGP session keys. + */ +public class PGPPad +{ + private PGPPad() + { + + } + + public static byte[] padSessionData(byte[] sessionInfo) + { + byte[] result = new byte[40]; + + System.arraycopy(sessionInfo, 0, result, 0, sessionInfo.length); + + byte padValue = (byte)(result.length - sessionInfo.length); + + for (int i = sessionInfo.length; i != result.length; i++) + { + result[i] = padValue; + } + + return result; + } + + public static byte[] unpadSessionData(byte[] encoded) + throws PGPException + { + byte padValue = encoded[encoded.length - 1]; + + for (int i = encoded.length - padValue; i != encoded.length; i++) + { + if (encoded[i] != padValue) + { + throw new PGPException("bad padding found in session data"); + } + } + + byte[] taggedKey = new byte[encoded.length - padValue]; + + System.arraycopy(encoded, 0, taggedKey, 0, taggedKey.length); + + return taggedKey; + } +} |