コード例 #1
0
ファイル: TSAConnector.java プロジェクト: CESNET/TSAConnector
  /**
   * computes digest hash using specified algorithm
   *
   * @param message base from which the digest will be computed (i.e. file to be stamped)
   * @param messageDigest algorithm used to calculate the digest
   * @return calculated digest
   */
  protected byte[] calculateMessageDigest(byte[] message, ExtendedDigest messageDigest) {
    messageDigest.update(message, 0, message.length); // offset - '0' means start from the beginning
    // digest obviously has to be computed from whole message
    byte[] digest = new byte[messageDigest.getDigestSize()];
    int size = messageDigest.doFinal(digest, 0); // offset - '0' means start from the beginning

    // return only valid part of digest (can be shorter than maximum digest size)
    return Arrays.copyOfRange(digest, 0, size); // offset - '0' means start from the beginning
  }
コード例 #2
0
ファイル: TSAConnector.java プロジェクト: CESNET/TSAConnector
  /**
   * Main method for stamping given file. It serves as an example of whole process in one go.
   *
   * @param filename
   * @param is
   * @return
   * @throws java.io.IOException
   * @throws org.bouncycastle.tsp.TSPException
   */
  public byte[] stamp(String filename, InputStream is) throws IOException, TSPException {
    logger.entry();

    logger.info("File to be stamped: {}", filename);
    byte[] file = getBytesFromInputStream(is);

    ExtendedDigest digestAlgorithm = new SHA256Digest(); // select hash algorithm
    ASN1ObjectIdentifier requestAlgorithm;
    try {
      requestAlgorithm = getHashObjectIdentifier(digestAlgorithm.getAlgorithmName());
    } catch (IllegalArgumentException e) {
      logger.catching(e);
      throw e;
    }
    logger.info("Selected algorithm: {}", digestAlgorithm.getAlgorithmName());

    // create request
    byte[] digest = calculateMessageDigest(file, digestAlgorithm);
    TimeStampRequest tsq = getTSRequest(digest, requestAlgorithm);
    logger.debug("TS request generated");

    // send request and receive response
    TimeStampResponse tsr;
    try {
      tsr = getTSResponse(tsq, server);
    } catch (IOException | TSPException e) {
      logger.catching(e);
      throw e;
    }
    logger.debug("TSA response received");

    // log reason of failure
    if (tsr.getFailInfo() != null) {
      logFailReason(tsr.getFailInfo().intValue());
      return null;
    }

    // log response
    logResponse(tsr);

    logger.exit();
    return tsr.getEncoded();
  }