當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Timestamp模塊用法及代碼示例

本文簡要介紹ruby語言中 OpenSSL::Timestamp模塊 的用法。

提供類和方法來請求、創建和驗證RFC3161-compliant 時間戳。 Request 可用於從頭開始創建請求或解析現有請求,這些請求可再次用於從時間戳服務器請求時間戳,例如通過網絡/http。可以使用 Response 解析生成的時間戳響應。

請注意, Response 是隻讀且不可變的。要創建 Response ,需要 Factory 的實例以及有效的 Request

創建響應:

#Assumes ts.p12 is a PKCS#12-compatible file with a private key
#and a certificate that has an extended key usage of 'timeStamping'
p12 = OpenSSL::PKCS12.new(File.binread('ts.p12'), 'pwd')
md = OpenSSL::Digest.new('SHA1')
hash = md.digest(data) #some binary data to be timestamped
req = OpenSSL::Timestamp::Request.new
req.algorithm = 'SHA1'
req.message_imprint = hash
req.policy_id = "1.2.3.4.5"
req.nonce = 42
fac = OpenSSL::Timestamp::Factory.new
fac.gen_time = Time.now
fac.serial_number = 1
timestamp = fac.create_timestamp(p12.key, p12.certificate, req)

驗證時間戳響應:

#Assume we have a timestamp token in a file called ts.der
ts = OpenSSL::Timestamp::Response.new(File.binread('ts.der'))
#Assume we have the Request for this token in a file called req.der
req = OpenSSL::Timestamp::Request.new(File.binread('req.der'))
# Assume the associated root CA certificate is contained in a
# DER-encoded file named root.cer
root = OpenSSL::X509::Certificate.new(File.binread('root.cer'))
# get the necessary intermediate certificates, available in
# DER-encoded form in inter1.cer and inter2.cer
inter1 = OpenSSL::X509::Certificate.new(File.binread('inter1.cer'))
inter2 = OpenSSL::X509::Certificate.new(File.binread('inter2.cer'))
ts.verify(req, root, inter1, inter2) -> ts or raises an exception if validation fails

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Timestamp模塊。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。