...

Source file src/gopkg.in/go-jose/go-jose.v2/utils_test.go

Documentation: gopkg.in/go-jose/go-jose.v2

     1  /*-
     2   * Copyright 2014 Square Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package jose
    18  
    19  import (
    20  	"crypto/rand"
    21  	"encoding/base64"
    22  	"encoding/hex"
    23  	"math/big"
    24  	"regexp"
    25  )
    26  
    27  // Reset random reader to original value
    28  func resetRandReader() {
    29  	RandReader = rand.Reader
    30  }
    31  
    32  // Build big int from hex-encoded string. Strips whitespace (for testing).
    33  func fromHexInt(base16 string) *big.Int {
    34  	re := regexp.MustCompile(`\s+`)
    35  	val, ok := new(big.Int).SetString(re.ReplaceAllString(base16, ""), 16)
    36  	if !ok {
    37  		panic("Invalid test data")
    38  	}
    39  	return val
    40  }
    41  
    42  // Build big int from base64-encoded string. Strips whitespace (for testing).
    43  func fromBase64Int(encoded string) *big.Int {
    44  	re := regexp.MustCompile(`\s+`)
    45  	val, err := base64.RawURLEncoding.DecodeString(re.ReplaceAllString(encoded, ""))
    46  	if err != nil {
    47  		panic("Invalid test data: " + err.Error())
    48  	}
    49  	return new(big.Int).SetBytes(val)
    50  }
    51  
    52  // Decode hex-encoded string into byte array. Strips whitespace (for testing).
    53  func fromHexBytes(base16 string) []byte {
    54  	re := regexp.MustCompile(`\s+`)
    55  	val, err := hex.DecodeString(re.ReplaceAllString(base16, ""))
    56  	if err != nil {
    57  		panic("Invalid test data")
    58  	}
    59  	return val
    60  }
    61  
    62  // Decode base64-encoded string into byte array. Strips whitespace (for testing).
    63  func fromBase64Bytes(b64 string) []byte {
    64  	re := regexp.MustCompile(`\s+`)
    65  	val, err := base64.StdEncoding.DecodeString(re.ReplaceAllString(b64, ""))
    66  	if err != nil {
    67  		panic("Invalid test data")
    68  	}
    69  	return val
    70  }
    71  
    72  // Decode base64-urlencoded string into byte array. Strips whitespace (for testing).
    73  func fromBase64URLBytes(b64 string) []byte {
    74  	re := regexp.MustCompile(`\s+`)
    75  	val, err := base64.RawURLEncoding.DecodeString(re.ReplaceAllString(b64, ""))
    76  	if err != nil {
    77  		panic("Invalid test data")
    78  	}
    79  	return val
    80  }
    81  

View as plain text