...

Source file src/github.com/digitorus/timestamp/timestamp_example_test.go

Documentation: github.com/digitorus/timestamp

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

View as plain text