1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
package timestamp_test
import (
"bytes"
"crypto"
"fmt"
"io"
"log"
"net/http"
"strings"
"github.com/digitorus/timestamp"
)
// ExampleCreateRequest_ParseResponse demonstrates the creation of a time-stamp request, sending
// it to the server and parsing the response.
// nolint: govet
func ExampleCreateRequest_ParseResponse() {
tsq, err := timestamp.CreateRequest(strings.NewReader("ExampleCreateRequestParseResponse"), ×tamp.RequestOptions{
Hash: crypto.SHA256,
Certificates: true,
})
if err != nil {
log.Fatal(err)
}
tsr, err := http.Post("https://freetsa.org/tsr", "application/timestamp-query", bytes.NewReader(tsq))
if err != nil {
log.Fatal(err)
}
if tsr.StatusCode > 200 {
log.Fatal(tsr.Status)
}
resp, err := io.ReadAll(tsr.Body)
if err != nil {
log.Fatal(err)
}
tsResp, err := timestamp.ParseResponse(resp)
if err != nil {
log.Fatal(err)
}
fmt.Println(tsResp.HashedMessage)
fmt.Println(tsResp.Policy)
for _, c := range tsResp.Certificates {
fmt.Println(c.Subject.Organization, c.Subject.OrganizationalUnit)
}
// Output:
// [140 222 43 143 28 80 96 97 4 176 145 205 188 119 197 142 149 101 26 96 188 163 178 64 230 162 199 171 176 178 173 128]
// 1.2.3.4.1
// [Free TSA] [TSA]
// [Free TSA] [Root CA]
}
|