github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/testonly/encoding.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testonly
    16  
    17  import (
    18  	"encoding/base64"
    19  	"encoding/hex"
    20  	"time"
    21  
    22  	"github.com/golang/protobuf/ptypes"
    23  	tspb "github.com/golang/protobuf/ptypes/timestamp"
    24  )
    25  
    26  // MustDecodeBase64 expects a base 64 encoded string input and panics if it cannot be decoded
    27  func MustDecodeBase64(b64 string) []byte {
    28  	r, err := base64.StdEncoding.DecodeString(b64)
    29  	if err != nil {
    30  		panic(r)
    31  	}
    32  	return r
    33  }
    34  
    35  // MustHexDecode decodes its input string from hex and panics if this fails
    36  func MustHexDecode(b string) []byte {
    37  	r, err := hex.DecodeString(b)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	return r
    42  }
    43  
    44  // MustToTimestampProto converts t to a Timestamp protobuf, or panics if this fails.
    45  func MustToTimestampProto(t time.Time) *tspb.Timestamp {
    46  	ret, err := ptypes.TimestampProto(t)
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  	return ret
    51  }