Package eu.europa.esig.dss.spi
Class DSSUtils
java.lang.Object
eu.europa.esig.dss.spi.DSSUtils
public final class DSSUtils extends Object
-
Field Summary
Fields Modifier and Type Field Description static byte[]
EMPTY_BYTE_ARRAY
-
Method Summary
Modifier and Type Method Description static boolean
compareFirstBytes(DSSDocument dssDocument, byte[] byteArray)
Reads firstbyteArray.length
bytes of thedssDocument
and compares them withbyteArray
static byte[]
concatenate(byte[]... arrays)
Concatenates all the arrays into a new array.static byte[]
convertToDER(String pemContent)
This method converts a PEM encoded certificate/crl/... to DER encodedstatic String
convertToPEM(CertificateToken cert)
This method converts the given certificate into its PEM string.static String
decodeUrl(String uri)
static byte[]
digest(DigestAlgorithm digestAlgorithm, byte[] data)
This method allows to digest the data with the given algorithm.static byte[]
digest(DigestAlgorithm digestAlgorithm, byte[]... data)
static byte[]
digest(DigestAlgorithm digestAlgorithm, DSSDocument document)
static byte[]
digest(DigestAlgorithm digestAlgo, InputStream inputStream)
This method allows to digest the data in theInputStream
with the given algorithm.static byte[]
encodeRSADigest(DigestAlgorithm digestAlgorithm, byte[] digest)
This method wraps the digest value in a DigestInfo (combination of digest algorithm and value).static String
encodeURI(String fileURI)
This method encodes an URI to be compliant with the RFC 3986 (see DSS-1475 for details)static String
formatDateWithCustomFormat(Date date, String format)
static String
formatInternal(Date date)
Formats a date to use for internal purposes (logging, toString)static String
getDeterministicId(Date signingTime, TokenIdentifier id)
Return a unique id for a date and the certificateToken id.static Digest
getDigest(DigestAlgorithm digestAlgo, DSSDocument dssDocument)
static String
getExceptionMessage(Exception e)
Returns a message retrieved from an exception, its cause message if the first is not defined, or exception class name if non of them is specifiedstatic long
getFileByteSize(DSSDocument dssDocument)
Returns byte size of the given documentstatic String
getMD5Digest(byte[] bytes)
Returns a Hex encoded of the MD5 digest of binariesstatic MessageDigest
getMessageDigest(DigestAlgorithm digestAlgorithm)
static String
getNormalizedString(String str)
This method replaces all special characters by an underscorestatic String
getSHA1Digest(String stringToDigest)
This method digests the given string with SHA1 algorithm and encode returned array of bytes as hex string.static Date
getUtcDate(int year, int month, int day)
This method returns an UTC date base on the year, the month and the day.static X500Principal
getX500PrincipalOrNull(String x500PrincipalString)
This method returns theX500Principal
corresponding to the given string ornull
if the conversion is not possible.static boolean
isStartWithASN1SequenceTag(InputStream is)
This method returns true if the inputStream starts with an ASN.1 Sequencestatic boolean
isTimestampToken(DSSDocument document)
Checks if the document contains a TimeStampTokenstatic CertificateToken
loadCertificate(byte[] input)
This method loads a certificate from the byte array.static CertificateToken
loadCertificate(File file)
This method loads a certificate from the given location.static CertificateToken
loadCertificate(InputStream inputStream)
This method loads a certificate from the given location.static CertificateToken
loadCertificateFromBase64EncodedString(String base64Encoded)
This method loads a certificate from a base 64 encoded Stringstatic Collection<CertificateToken>
loadCertificateFromP7c(InputStream is)
static Collection<CertificateToken>
loadPotentialIssuerCertificates(CertificateToken cert, DataLoader loader)
This method loads the potential issuer certificate(s) from the given locations (AIA).static void
printSecurityProviders()
This method lists all defined security providers.static long
readAvailableBytes(DSSDocument dssDocument, byte[] b)
Read the requested number of bytes fromDSSDocument
according to the size of the providedbyte
[] buffer and validates success of the operationstatic long
readAvailableBytes(InputStream is, byte[] b)
Read the requested number of bytes fromInputStream
according to the size of the providedbyte
[] buffer and validates success of the operationstatic long
readAvailableBytes(InputStream is, byte[] b, int off, int len)
Read the requested number of bytes fromInputStream
and validates success of the operationstatic byte
readFirstByte(DSSDocument dssDocument)
Reads the first byte from the DSSDocumentstatic String
removeControlCharacters(String str)
Replaces null ASCII characters 00-31 and 127 with ''static void
saveToFile(byte[] bytes, File file)
This method saves the given array ofbyte
to the providedFile
.static long
skipAvailableBytes(InputStream is, long n)
Skip the definedn
number of bytes from theInputStream
and validates success of the operationstatic DSSDocument
splitDocument(DSSDocument origin, int start, int end)
This method create a new document from a sub-part of another documentstatic byte[]
toByteArray(DSSDocument document)
Get the contents of anDSSDocument
as abyte[]
.static byte[]
toByteArray(File file)
FROM: Apache Reads the contents of a file into a byte array.static byte[]
toByteArray(InputStream inputStream)
Get the contents of anInputStream
as abyte[]
.static InputStream
toByteArrayInputStream(File file)
This method returns anInputStream
which does not need to be closed, based onByteArrayInputStream
.static org.bouncycastle.cms.CMSSignedData
toCMSSignedData(DSSDocument document)
Gets CMSSignedData from thedocument
bytesstatic String
toHex(byte[] value)
Converts an array of bytes into a String representing the hexadecimal values of each byte in order.static InputStream
toInputStream(File file)
This method returns anInputStream
which needs to be closed, based onFileInputStream
.
-
Field Details
-
EMPTY_BYTE_ARRAY
public static final byte[] EMPTY_BYTE_ARRAY
-
-
Method Details
-
formatInternal
Formats a date to use for internal purposes (logging, toString)- Parameters:
date
- the date to be converted- Returns:
- the textual representation (a null date will result in "N/A")
-
formatDateWithCustomFormat
-
toHex
Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned String will be double the length of the passed array, as it takes two characters to represent any given byte. If the input array is null then null is returned. The obtained string is converted to uppercase.- Parameters:
value
- the value to be converted to hexadecimal- Returns:
- the hexadecimal String
-
convertToPEM
This method converts the given certificate into its PEM string.- Parameters:
cert
- the token to be converted to PEM- Returns:
- PEM encoded certificate
-
isStartWithASN1SequenceTag
This method returns true if the inputStream starts with an ASN.1 Sequence- Parameters:
is
- the inputstream to be tested- Returns:
- true if DER encoded
-
convertToDER
This method converts a PEM encoded certificate/crl/... to DER encoded- Parameters:
pemContent
- the String which contains the PEM encoded object- Returns:
- the binaries of the DER encoded object
-
loadCertificate
This method loads a certificate from the given location. The certificate must be DER-encoded and may be supplied in binary or printable (PEM / Base64) encoding. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by-----BEGIN CERTIFICATE-----
, and must be bounded at the end by-----END CERTIFICATE-----
.- Parameters:
file
- the file with the certificate- Returns:
- the certificate token
-
loadCertificate
This method loads a certificate from the given location. The certificate must be DER-encoded and may be supplied in binary or printable (PEM / Base64) encoding. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by-----BEGIN CERTIFICATE-----
, and must be bounded at the end by-----END CERTIFICATE-----
.- Parameters:
inputStream
- input stream containing the certificate- Returns:
- the certificate token
-
loadCertificateFromP7c
-
loadCertificate
This method loads a certificate from the byte array. The certificate must be DER-encoded and may be supplied in binary or printable (Base64) encoding. If the certificate is provided in Base64 encoding, it must be bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by -----END CERTIFICATE-----. It throws anDSSException
or returnnull
when the certificate cannot be loaded.- Parameters:
input
- array of bytes containing the certificate- Returns:
- the certificate token
-
loadCertificateFromBase64EncodedString
This method loads a certificate from a base 64 encoded String- Parameters:
base64Encoded
- the base64 encoded certificate- Returns:
- the certificate token
-
loadPotentialIssuerCertificates
public static Collection<CertificateToken> loadPotentialIssuerCertificates(CertificateToken cert, DataLoader loader)This method loads the potential issuer certificate(s) from the given locations (AIA).- Parameters:
cert
- certificate for which the issuer(s) should be loadedloader
- the data loader to use- Returns:
- a list of potential issuers
-
getSHA1Digest
This method digests the given string with SHA1 algorithm and encode returned array of bytes as hex string.- Parameters:
stringToDigest
- Everything in the name- Returns:
- hex encoded digest value
-
digest
This method allows to digest the data with the given algorithm.- Parameters:
digestAlgorithm
- the algorithm to usedata
- the data to digest- Returns:
- digested array of bytes
-
getMessageDigest
-
encodeRSADigest
This method wraps the digest value in a DigestInfo (combination of digest algorithm and value). This encapsulation is required to operate NONEwithRSA signatures.- Parameters:
digestAlgorithm
- the used digest algorithmdigest
- the digest value- Returns:
- DER encoded binaries of the related digest info
-
digest
This method allows to digest the data in theInputStream
with the given algorithm.- Parameters:
digestAlgo
- the algorithm to useinputStream
- the data to digest- Returns:
- digested array of bytes
-
digest
-
digest
-
toInputStream
This method returns anInputStream
which needs to be closed, based onFileInputStream
.- Parameters:
file
-File
to read.- Returns:
- an
InputStream
materialized by aFileInputStream
representing the contents of the file @ if an I/O error occurred
-
toByteArrayInputStream
This method returns anInputStream
which does not need to be closed, based onByteArrayInputStream
.- Parameters:
file
-File
to read- Returns:
InputStream
based onByteArrayInputStream
-
toByteArray
FROM: Apache Reads the contents of a file into a byte array. The file is always closed.- Parameters:
file
- the file to read, must not benull
- Returns:
- the file contents, never
null
-
splitDocument
This method create a new document from a sub-part of another document- Parameters:
origin
- the original documentstart
- the start position to retrieveend
- the end position to retrieve- Returns:
- a new DSSDocument
-
toByteArray
Get the contents of anDSSDocument
as abyte[]
.- Parameters:
document
- the document to read- Returns:
- the content as byte array
-
toByteArray
Get the contents of anInputStream
as abyte[]
.- Parameters:
inputStream
- the inputstream to read- Returns:
- the content of the inputstream as byte array
-
toCMSSignedData
Gets CMSSignedData from thedocument
bytes- Parameters:
document
-DSSDocument
contained CMSSignedData- Returns:
CMSSignedData
-
isTimestampToken
Checks if the document contains a TimeStampToken- Parameters:
document
- theDSSDocument
to be checked- Returns:
- true if the document is a timestamp
-
getFileByteSize
Returns byte size of the given document- Parameters:
dssDocument
-DSSDocument
to get size for- Returns:
- long size of the given document
-
saveToFile
This method saves the given array ofbyte
to the providedFile
.- Parameters:
bytes
- the binary to savefile
- the file where to store
-
getNormalizedString
This method replaces all special characters by an underscore- Parameters:
str
- the string / filename / url to normalize- Returns:
- the normalized
String
-
getDeterministicId
Return a unique id for a date and the certificateToken id.- Parameters:
signingTime
- the signing timeid
- the token identifier- Returns:
- a unique string
-
getMD5Digest
Returns a Hex encoded of the MD5 digest of binaries- Parameters:
bytes
- the bytes to be digested- Returns:
- the hex encoded MD5 digest
-
getX500PrincipalOrNull
This method returns theX500Principal
corresponding to the given string ornull
if the conversion is not possible.- Parameters:
x500PrincipalString
- aString
representation of theX500Principal
- Returns:
X500Principal
or null
-
getUtcDate
This method returns an UTC date base on the year, the month and the day. The year must be encoded as 1978... and not 78- Parameters:
year
- the value used to set the YEAR calendar field.month
- the month. Month value is 0-based. e.g., 0 for January.day
- the value used to set the DAY_OF_MONTH calendar field.- Returns:
- the UTC date base on parameters
-
printSecurityProviders
public static void printSecurityProviders()This method lists all defined security providers. -
readFirstByte
Reads the first byte from the DSSDocument- Parameters:
dssDocument
- the document- Returns:
- the first byte
-
compareFirstBytes
Reads firstbyteArray.length
bytes of thedssDocument
and compares them withbyteArray
- Parameters:
dssDocument
-DSSDocument
to read bytes frombyteArray
-byte
array to compare the beginning string with- Returns:
- TRUE if the document starts from
byteArray
, FALSE otherwise
-
concatenate
public static byte[] concatenate(byte[]... arrays)Concatenates all the arrays into a new array. The new array contains all of the element of each array followed by all of the elements of the next array. When an array is returned, it is always a new array.- Parameters:
arrays
-byte
arrays to concatenate- Returns:
- the new
byte
array
-
decodeUrl
-
skipAvailableBytes
Skip the definedn
number of bytes from theInputStream
and validates success of the operation- Parameters:
is
-InputStream
to skip bytes fromn
-long
number bytes to skip- Returns:
- actual number of bytes have been skipped
- Throws:
IllegalStateException
- in case ofInputStream
reading error
-
readAvailableBytes
public static long readAvailableBytes(DSSDocument dssDocument, byte[] b) throws IllegalStateExceptionRead the requested number of bytes fromDSSDocument
according to the size of the providedbyte
[] buffer and validates success of the operation- Parameters:
dssDocument
-DSSDocument
to read bytes fromb
-byte
[] buffer to fill- Returns:
- the total number of bytes read into buffer
- Throws:
IllegalStateException
- in case ofInputStream
reading error
-
readAvailableBytes
Read the requested number of bytes fromInputStream
according to the size of the providedbyte
[] buffer and validates success of the operation- Parameters:
is
-InputStream
to read bytes fromb
-byte
[] buffer to fill- Returns:
- the total number of bytes read into buffer
- Throws:
IllegalStateException
- in case ofInputStream
reading error
-
readAvailableBytes
public static long readAvailableBytes(InputStream is, byte[] b, int off, int len) throws IllegalStateExceptionRead the requested number of bytes fromInputStream
and validates success of the operation- Parameters:
is
-InputStream
to read bytes fromb
-byte
[] buffer to filloff
-int
offset in the destination arraylen
-int
number of bytes to read- Returns:
- the total number of bytes read into buffer
- Throws:
IllegalStateException
- in case ofInputStream
reading error
-
encodeURI
This method encodes an URI to be compliant with the RFC 3986 (see DSS-1475 for details)- Parameters:
fileURI
- the uri to be encoded- Returns:
- the encoded result
-
getExceptionMessage
Returns a message retrieved from an exception, its cause message if the first is not defined, or exception class name if non of them is specified -
getDigest
-
removeControlCharacters
Replaces null ASCII characters 00-31 and 127 with ''
-