Class DSSUtils

java.lang.Object
eu.europa.esig.dss.spi.DSSUtils

public final class DSSUtils
extends Object
  • Field Details

  • Method Details

    • formatInternal

      public static String formatInternal​(Date date)
      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

      public static String formatDateWithCustomFormat​(Date date, String format)
    • toHex

      public static String toHex​(byte[] value)
      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
    • toDigit

      protected static int toDigit​(char ch, int index) throws DSSException
      Converts a hexadecimal character to an integer.
      Parameters:
      ch - A character to convert to an integer digit
      index - The index of the character in the source
      Returns:
      An integer
      Throws:
      DSSException - Thrown if ch is an illegal hex character
    • resourceExists

      public static boolean resourceExists​(String path)
      This method checks if the resource with the given path exists.
      Parameters:
      path -
      Returns:
    • fileExists

      public static boolean fileExists​(String path)
      This method checks if the file with the given path exists.
      Parameters:
      path -
      Returns:
    • getFile

      public static File getFile​(String filePath)
      This method returns a file reference. The file path is normalised (OS independent)
      Parameters:
      filePath - The path to the file.
      Returns:
    • convertToPEM

      public static String convertToPEM​(CertificateToken cert)
      This method converts the given certificate into its PEM string.
      Parameters:
      cert - the token to be converted to PEM
      Returns:
      PEM encoded certificate
    • isStartWithASN1SequenceTag

      public static boolean isStartWithASN1SequenceTag​(InputStream is)
      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

      public static byte[] convertToDER​(String pemContent)
      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

      public static CertificateToken loadCertificate​(File file)
      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

      public static CertificateToken loadCertificate​(InputStream inputStream)
      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

      public static Collection<CertificateToken> loadCertificateFromP7c​(InputStream is)
    • loadCertificate

      public static CertificateToken loadCertificate​(byte[] input)
      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 an DSSException or return null when the certificate cannot be loaded.
      Parameters:
      input - array of bytes containing the certificate
      Returns:
      the certificate token
    • loadCertificateFromBase64EncodedString

      public static CertificateToken loadCertificateFromBase64EncodedString​(String base64Encoded)
      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 loaded
      loader - the data loader to use
      Returns:
      a list of potential issuers
    • getSHA1Digest

      public static String getSHA1Digest​(String stringToDigest)
      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

      public static byte[] digest​(eu.europa.esig.dss.enumerations.DigestAlgorithm digestAlgorithm, byte[] data)
      This method allows to digest the data with the given algorithm.
      Parameters:
      digestAlgorithm - the algorithm to use
      data - the data to digest
      Returns:
      digested array of bytes
    • getMessageDigest

      public static MessageDigest getMessageDigest​(eu.europa.esig.dss.enumerations.DigestAlgorithm digestAlgorithm)
    • encodeRSADigest

      public static byte[] encodeRSADigest​(eu.europa.esig.dss.enumerations.DigestAlgorithm digestAlgorithm, byte[] digest)
      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 algorithm
      digest - the digest value
      Returns:
      DER encoded binaries of the related digest info
    • digest

      public static byte[] digest​(eu.europa.esig.dss.enumerations.DigestAlgorithm digestAlgo, InputStream inputStream)
      This method allows to digest the data in the InputStream with the given algorithm.
      Parameters:
      digestAlgo - the algorithm to use
      inputStream - the data to digest
      Returns:
      digested array of bytes
    • digest

      public static byte[] digest​(eu.europa.esig.dss.enumerations.DigestAlgorithm digestAlgorithm, DSSDocument document)
    • digest

      public static byte[] digest​(eu.europa.esig.dss.enumerations.DigestAlgorithm digestAlgorithm, byte[]... data)
    • toInputStream

      public static InputStream toInputStream​(String filePath) throws DSSException
      This method returns an InputStream which needs to be closed, based on FileInputStream.
      Parameters:
      filePath - The path to the file to read
      Returns:
      an InputStream materialized by a FileInputStream representing the contents of the file
      Throws:
      DSSException
    • toInputStream

      public static InputStream toInputStream​(File file)
      This method returns an InputStream which needs to be closed, based on FileInputStream.
      Parameters:
      file - File to read.
      Returns:
      an InputStream materialized by a FileInputStream representing the contents of the file @ if an I/O error occurred
    • toInputStream

      public static InputStream toInputStream​(String string, String charset) throws DSSException
      This method returns the InputStream based on the given String and char set. This stream does not need to be closed, it is based on ByteArrayInputStream.
      Parameters:
      string - String to convert
      charset - char set to use
      Returns:
      the InputStream based on ByteArrayInputStream
      Throws:
      DSSException
    • toFileOutputStream

      public static FileOutputStream toFileOutputStream​(String path) throws DSSException
      This method returns a FileOutputStream based on the provided path to the file.
      Parameters:
      path - to the file
      Returns:
      FileOutputStream
      Throws:
      DSSException
    • toByteArrayInputStream

      public static InputStream toByteArrayInputStream​(File file)
      This method returns an InputStream which does not need to be closed, based on ByteArrayInputStream.
      Parameters:
      file - File to read
      Returns:
      InputStream based on ByteArrayInputStream
    • toByteArray

      public static byte[] toByteArray​(File file)
      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 be null
      Returns:
      the file contents, never null
    • splitDocument

      public static DSSDocument splitDocument​(DSSDocument origin, int start, int end)
      This method create a new document from a sub-part of another document
      Parameters:
      origin - the original document
      start - the start position to retrieve
      end - the end position to retrieve
      Returns:
      a new DSSDocument
    • toByteArray

      public static byte[] toByteArray​(DSSDocument document)
      Get the contents of an DSSDocument as a byte[].
      Parameters:
      document - the document to read
      Returns:
      the content as byte array
    • toByteArray

      public static byte[] toByteArray​(InputStream inputStream)
      Get the contents of an InputStream as a byte[].
      Parameters:
      inputStream - the inputstream to read
      Returns:
      the content of the inputstream as byte array
    • toCMSSignedData

      public static org.bouncycastle.cms.CMSSignedData toCMSSignedData​(DSSDocument document)
      Gets CMSSignedData from the document bytes
      Parameters:
      document - DSSDocument contained CMSSignedData
      Returns:
      CMSSignedData
    • isTimestampToken

      public static boolean isTimestampToken​(DSSDocument document)
      Checks if the document contains a TimeStampToken
      Parameters:
      document - the DSSDocument to be checked
      Returns:
      true if the document is a timestamp
    • getFileByteSize

      public static long getFileByteSize​(DSSDocument dssDocument)
      Returns byte size of the given document
      Parameters:
      dssDocument - DSSDocument to get size for
      Returns:
      long size of the given document
    • saveToFile

      public static void saveToFile​(byte[] bytes, File file)
      This method saves the given array of byte to the provided File.
      Parameters:
      bytes - the binary to save
      file - the file where to store
    • getNormalizedString

      public static String getNormalizedString​(String str)
      This method replaces all special characters by an underscore
      Parameters:
      str - the string / filename / url to normalize
      Returns:
      the normalized String
    • getDeterministicId

      public static String getDeterministicId​(Date signingTime, TokenIdentifier id)
      Return a unique id for a date and the certificateToken id.
      Parameters:
      signingTime - the signing time
      id - the token identifier
      Returns:
      a unique string
    • getMD5Digest

      public static String getMD5Digest​(byte[] bytes)
      Returns a Hex encoded of the MD5 digest of binaries
      Parameters:
      bytes - the bytes to be digested
      Returns:
      the hex encoded MD5 digest
    • getX500PrincipalOrNull

      public static X500Principal getX500PrincipalOrNull​(String x500PrincipalString)
      This method returns the X500Principal corresponding to the given string or null if the conversion is not possible.
      Parameters:
      x500PrincipalString - a String representation of the X500Principal
      Returns:
      X500Principal or null
    • getUtcDate

      public static Date getUtcDate​(int year, int month, int day)
      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
    • getMessageId

      public static String getMessageId​(String message)
      This method return the unique message id which can be used for translation purpose.
      Parameters:
      message - the String message on which the unique id is calculated.
      Returns:
      the unique id
    • available

      public static int available​(InputStream is) throws DSSException
      Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes. the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream. IOException if this input stream has been closed by invoking the InputStream.close() method. returns 0.
      Returns:
      an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.
      Throws:
      DSSException - if IOException occurs (if an I/O error occurs)
    • printSecurityProviders

      public static void printSecurityProviders()
      This method lists all defined security providers.
    • readFirstByte

      public static byte readFirstByte​(DSSDocument dssDocument)
      Reads the first byte from the DSSDocument
      Parameters:
      dssDocument - the document
      Returns:
      the first byte
    • compareFirstBytes

      public static boolean compareFirstBytes​(DSSDocument dssDocument, byte[] byteArray)
      Reads first byteArray.length bytes of the dssDocument and compares them with byteArray
      Parameters:
      dssDocument - DSSDocument to read bytes from
      byteArray - 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

      public static String decodeUrl​(String uri)
    • skipAvailableBytes

      public static long skipAvailableBytes​(InputStream is, long n) throws IllegalStateException
      Skip the defined n number of bytes from the InputStream and validates success of the operation
      Parameters:
      is - InputStream to skip bytes from
      n - long number bytes to skip
      Returns:
      actual number of bytes have been skipped
      Throws:
      IllegalStateException - in case of InputStream reading error
    • readAvailableBytes

      public static long readAvailableBytes​(DSSDocument dssDocument, byte[] b) throws IllegalStateException
      Read the requested number of bytes from DSSDocument according to the size of the provided byte[] buffer and validates success of the operation
      Parameters:
      dssDocument - DSSDocument to read bytes from
      b - byte[] buffer to fill
      Returns:
      the total number of bytes read into buffer
      Throws:
      IllegalStateException - in case of InputStream reading error
    • readAvailableBytes

      public static long readAvailableBytes​(InputStream is, byte[] b) throws IllegalStateException
      Read the requested number of bytes from InputStream according to the size of the provided byte[] buffer and validates success of the operation
      Parameters:
      is - InputStream to read bytes from
      b - byte[] buffer to fill
      Returns:
      the total number of bytes read into buffer
      Throws:
      IllegalStateException - in case of InputStream reading error
    • readAvailableBytes

      public static long readAvailableBytes​(InputStream is, byte[] b, int off, int len) throws IllegalStateException
      Read the requested number of bytes from InputStream and validates success of the operation
      Parameters:
      is - InputStream to read bytes from
      b - byte[] buffer to fill
      off - int offset in the destination array
      len - int number of bytes to read
      Returns:
      the total number of bytes read into buffer
      Throws:
      IllegalStateException - in case of InputStream reading error
    • encodeURI

      public static String encodeURI​(String fileURI)
      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

      public 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 specified
      Parameters:
      e - Exception to get message for
      Returns:
      String exception message
    • getDigest

      public static Digest getDigest​(eu.europa.esig.dss.enumerations.DigestAlgorithm digestAlgo, DSSDocument dssDocument)
    • removeControlCharacters

      public static String removeControlCharacters​(String str)
      Replaces null ASCII characters 00-31 and 127 with ''
      Parameters:
      str - String to remove Ctrls characters from
      Returns:
      String without Ctrls characters