2015-01-22 52 views

답변

5

귀하의 질문은 약간 넓습니다 ... 나는 당신에게 올바른 방향으로 당신을 가리킬 것이라고 희망 몇 가지 정보를 줄 것입니다.

거기에 서비스를 사용하여 타임 스탬프 서비스를 사용하려면 타임 스탬프 서비스를 사용하고 싶습니다. http://timestamp.globalsign.com/scripts/timstamp.dll.

우선이 서비스는 Time-Stamp Protocol (TSP) RFC3161 컴파일러이며, RFC definition here에서 어떻게 작동하는지 명확하게 알 수 있습니다.

임의로 자바 코드 예제를 찾고 있다고 생각합니다. 아래에서는 RFC3161의 타임 스탬프 서버를 사용하여 타임 스탬프 서명을 수행하는 샘플 코드를 제공합니다.

기본적으로이 샘플 단계는 다음과 같습니다

  1. 먼저 다음 서비스에 요청을 보내, 타임 스탬프 요청을 만들고 마지막으로 응답을 읽습니다. 당신이 볼 수 있듯이

    TimeStampReq ::= SEQUENCE { 
        version      INTEGER { v1(1) }, 
        messageImprint    MessageImprint, 
        --a hash algorithm OID and the hash value of the data to be time-stamped 
        reqPolicy    TSAPolicyId    OPTIONAL, 
        nonce     INTEGER     OPTIONAL, 
        certReq    BOOLEAN     DEFAULT FALSE, 
        extensions   [0] IMPLICIT Extensions OPTIONAL } 
    

    만이 필요한 년대 messageImprint, 나머지는 선택 사항 입니다 당신에게 제공하여 술 서비스의 옵션에 따라 달라집니다

    타임 스탬프 요청은 다음과 정의를 가지고있다. application/timestamp-query :

  2. 번째 단계는 Content-type HTTP 헤더로 지정 POST의 메소드 사용이 소인 요구를 송신한다.

  3. 마지막 부분은 응답을 구문 분석하고 타임 스탬프 토큰을 얻을 수 있습니다. 모두 함께

    : 내가 샘플에서 bouncycastle API을 사용

    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; 
        } 
    } 
    

그래서 여기에 코드입니다. 이 도움이

희망,

+0

답장을 보내 주셔서 감사 많이 @albciff ......하지만 난 이미 나와 함께이 article.The 문제를 겪었 난 그냥 자바 API와 외력을 사용해야한다는 것입니다 항아리 또는 API를 사용할 수 있습니다. 동일한 해결 방법을 알려주시겠습니까? 이 기사를 통해 –

+0

? 너 무슨 뜻이야 ...어쨌든'TimeStampReq' 구조체 요소와 다른 관련 asn1 객체를'bouncycastle'없이 구현하고 싶다면 ... RFC 사양을 읽어서 그것을 구현하십시오 ... – albciff