首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用TSA和Java的时间戳

使用TSA和Java的时间戳
EN

Stack Overflow用户
提问于 2015-01-22 09:51:33
回答 1查看 7.2K关注 0票数 2

请任何人帮助我理解签名时使用的流程和Java。

我需要使用使用Java的TSA url "http://timestamp.globalsign.com/scripts/timstamp.dll“对文件进行签名和时间戳。

我能够使用java.security API对文件进行签名,但无法对其进行时间戳。

EN

回答 1

Stack Overflow用户

发布于 2015-01-23 14:07:14

你的问题有点宽泛..。我会给你一些信息,我希望它能为你指明正确的方向。

问题是,您希望使用时间戳服务来使用服务执行时间戳签名:http://timestamp.globalsign.com/scripts/timstamp.dll

首先,这个服务是一个Time-Stamp Protocol (TSP) RFC3161编译器,查看一下RFC definition here,了解一下它是如何工作的。

无论如何,我认为您正在寻找一个java代码示例,因此下面我给出一个示例代码,它使用RFC3161的时间戳服务器执行时间戳签名。

基本上,本示例中的步骤如下:

  1. 首先创建时间戳请求,然后将请求发送到服务,最后读取响应。 时间戳请求具有以下定义: TimeStampReq ::=序列{版本整数{ v1(1) },messageImprint MessageImprint,--哈希算法OID和数据的哈希值为时间戳reqPolicy TSAPolicyId可选,非整数可选,certReq布尔值默认为FALSE,扩展隐式扩展可选} 正如您只能看到所需的messageImprint一样,其余的都是可选的,这取决于tsp服务提供给您的选项。

  1. 第二步是使用POST方法发送这个时间戳请求,指定为Content-type http-header:application/timestamp-query

  1. 最后一部分是解析响应并获取时间戳令牌。

下面是代码:

合在一起:

代码语言:javascript
复制
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Date;
import java.util.Random;

import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1StreamParser;
import org.bouncycastle.asn1.DERBoolean;
import org.bouncycastle.asn1.DERInteger;
import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.asn1.tsp.MessageImprint;
import org.bouncycastle.asn1.tsp.TimeStampReq;
import org.bouncycastle.asn1.tsp.TimeStampResp;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.tsp.TimeStampResponse;
import org.bouncycastle.tsp.TimeStampToken;

public class TimeStampGenerationSample {

    public static void main(String args[]) throws Exception{

        // for this sample we will use SHA1 to perform the hashes
        // however feel free to use another algorithm since sha1 is weakness
        String sha1Oid = "1.3.14.3.2.26";
        // data to be timestamped
        byte[] data = "some sample data... or your signature...".getBytes();

        // perform the hash of your data
        byte[] digestData = MessageDigest.getInstance(sha1Oid, new BouncyCastleProvider()).digest(data);
        // generate random data to perform your ts, it's optional depends on your ts service
        Random rand = new Random(new Date().getTime()); 
        String nonce = BigInteger.valueOf(rand.nextLong()).toString();          
        // require cert optional (default false... so use false)
        boolean requireCert = false;
        // timestampPolicy it's an oid to identify a policy, if it's required
        // must be provided by your ts service... it's optional so we put null
        String timestampPolicy = null;      

        TimeStampReq ts_req = createTimeStampRequest(digestData, nonce, requireCert, sha1Oid, timestampPolicy);

        // the data to be send to the service
        byte[] dataToSend = ts_req.getEncoded();

        // simply send your data using POST method
        // don't forget to specify http-header content-type as "application/timestamp-query"
        byte[] response = // send the request as you want
        // parse the response 
        ASN1StreamParser asn1Sp = new ASN1StreamParser(response);
        TimeStampResp tspResp = new TimeStampResp((ASN1Sequence)asn1Sp.readObject());
        TimeStampResponse tsr = new TimeStampResponse(tspResp);
        // and get the timestamp token :)
        TimeStampToken token = tsr.getTimeStampToken();
    }

    /**
     * Create the timestamp request
     * @param hashedData
     * @param nonce
     * @param requireCert
     * @param digestAlgorithm
     * @param timestampPolicy
     * @return
     * @throws TimeStampGenerationException
     */
    public static TimeStampReq createTimeStampRequest(byte[] hashedData, String nonce, boolean requireCert, String digestAlgorithm, String timestampPolicy) throws TimeStampGenerationException {

        MessageImprint imprint = new MessageImprint(new AlgorithmIdentifier(digestAlgorithm), hashedData);

        TimeStampReq request = new TimeStampReq(
                imprint, 
                timestampPolicy!=null?new DERObjectIdentifier(timestampPolicy):null, 
                nonce!=null?new DERInteger(nonce.getBytes()):null, 
                new DERBoolean(requireCert), 
                null
        );      

        return request;
    }
}

注意,我在示例中使用了bouncycastle API

希望这能帮上忙

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28085619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档