-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,669 additions
and
16 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
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
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
37 changes: 37 additions & 0 deletions
37
src/main/java/eu/europa/ec/dgc/generation/Base45Encoder.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,37 @@ | ||
package eu.europa.ec.dgc.generation; | ||
|
||
/** | ||
* Base45 Encoder. | ||
*/ | ||
public class Base45Encoder { | ||
private static final String ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; | ||
|
||
private Base45Encoder() { | ||
} | ||
|
||
/** | ||
* Encode a byte array to Base45 String. | ||
* | ||
* @param bytes bytes to encode | ||
* @return encoded string | ||
*/ | ||
public static String encodeToString(byte[] bytes) { | ||
StringBuilder result = new StringBuilder(); | ||
for (int i = 0; i < bytes.length; i += 2) { | ||
if (bytes.length - i > 1) { | ||
int x = ((bytes[i] & 0xFF) << 8) + (bytes[i + 1] & 0xFF); | ||
int e = x / (45 * 45); | ||
int y = x % (45 * 45); | ||
int d = y / 45; | ||
int c = y % 45; | ||
result.append(ALPHABET.charAt(c)).append(ALPHABET.charAt(d)).append(ALPHABET.charAt(e)); | ||
} else { | ||
int x = bytes[i] & 0xFF; | ||
int d = x / 45; | ||
int c = x % 45; | ||
result.append(ALPHABET.charAt(c)).append(ALPHABET.charAt(d)); | ||
} | ||
} | ||
return result.toString(); | ||
} | ||
} |
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,98 @@ | ||
package eu.europa.ec.dgc.generation; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import org.bouncycastle.crypto.Digest; | ||
import org.bouncycastle.crypto.digests.SHA256Digest; | ||
import org.bouncycastle.util.Arrays; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CopyDigest implements Digest { | ||
|
||
private final OpenByteArrayOutputStream binaryOut = new OpenByteArrayOutputStream(); | ||
private final SHA256Digest sha256Digest = new SHA256Digest(); | ||
private boolean wasReset = false; | ||
|
||
public String getAlgorithmName() { | ||
return "NULL"; | ||
} | ||
|
||
public int getDigestSize() { | ||
return 32; | ||
} | ||
|
||
/** | ||
* Updates the Message Digest with one byte. | ||
* | ||
* @param in byte to update. | ||
*/ | ||
public void update(byte in) { | ||
if (wasReset) { | ||
sha256Digest.update(in); | ||
} else { | ||
binaryOut.write(in); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the Message Digest with a byte array. | ||
* | ||
* @param in byte array to insert | ||
* @param offset Offset | ||
* @param length length | ||
*/ | ||
public void update(byte[] in, int offset, int length) { | ||
if (wasReset) { | ||
sha256Digest.update(in, offset, length); | ||
} else { | ||
binaryOut.write(in, offset, length); | ||
} | ||
} | ||
|
||
/** | ||
* close the digest, producing the final digest value. The doFinal | ||
* call leaves the digest reset. | ||
* | ||
* @param out the array the digest is to be copied into. | ||
* @param offset the offset into the out array the digest is to start at. | ||
* @return size of output | ||
*/ | ||
public int doFinal(byte[] out, int offset) { | ||
if (wasReset) { | ||
return sha256Digest.doFinal(out, offset); | ||
} else { | ||
int size = binaryOut.size(); | ||
binaryOut.copy(out, offset); | ||
reset(); | ||
return size; | ||
} | ||
} | ||
|
||
/** | ||
* Resets the Message Digest. | ||
*/ | ||
@Override | ||
public void reset() { | ||
if (wasReset) { | ||
sha256Digest.reset(); | ||
} else { | ||
if (binaryOut.size() > 0) { | ||
wasReset = true; | ||
binaryOut.reset(); | ||
} | ||
} | ||
} | ||
|
||
private static class OpenByteArrayOutputStream | ||
extends ByteArrayOutputStream { | ||
public synchronized void reset() { | ||
super.reset(); | ||
|
||
Arrays.clear(buf); | ||
} | ||
|
||
void copy(byte[] out, int outOff) { | ||
System.arraycopy(buf, 0, out, outOff, this.size()); | ||
} | ||
} | ||
} |
Oops, something went wrong.