...

Source file src/github.com/aliyun/credentials-go/credentials/utils/utils.go

Documentation: github.com/aliyun/credentials-go/credentials/utils

     1  package utils
     2  
     3  import (
     4  	"crypto"
     5  	"crypto/hmac"
     6  	"crypto/md5"
     7  	"crypto/rand"
     8  	"crypto/rsa"
     9  	"crypto/sha1"
    10  	"crypto/x509"
    11  	"encoding/base64"
    12  	"encoding/hex"
    13  	"hash"
    14  	"io"
    15  	rand2 "math/rand"
    16  	"net/url"
    17  	"time"
    18  )
    19  
    20  type uuid [16]byte
    21  
    22  const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    23  
    24  var hookRead = func(fn func(p []byte) (n int, err error)) func(p []byte) (n int, err error) {
    25  	return fn
    26  }
    27  
    28  var hookRSA = func(fn func(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error)) func(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error) {
    29  	return fn
    30  }
    31  
    32  // GetUUID returns a uuid
    33  func GetUUID() (uuidHex string) {
    34  	uuid := newUUID()
    35  	uuidHex = hex.EncodeToString(uuid[:])
    36  	return
    37  }
    38  
    39  // RandStringBytes returns a rand string
    40  func RandStringBytes(n int) string {
    41  	b := make([]byte, n)
    42  	for i := range b {
    43  		b[i] = letterBytes[rand2.Intn(len(letterBytes))]
    44  	}
    45  	return string(b)
    46  }
    47  
    48  // ShaHmac1 return a string which has been hashed
    49  func ShaHmac1(source, secret string) string {
    50  	key := []byte(secret)
    51  	hmac := hmac.New(sha1.New, key)
    52  	hmac.Write([]byte(source))
    53  	signedBytes := hmac.Sum(nil)
    54  	signedString := base64.StdEncoding.EncodeToString(signedBytes)
    55  	return signedString
    56  }
    57  
    58  // Sha256WithRsa return a string which has been hashed with Rsa
    59  func Sha256WithRsa(source, secret string) string {
    60  	decodeString, err := base64.StdEncoding.DecodeString(secret)
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	private, err := x509.ParsePKCS8PrivateKey(decodeString)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  
    69  	h := crypto.Hash.New(crypto.SHA256)
    70  	h.Write([]byte(source))
    71  	hashed := h.Sum(nil)
    72  	signature, err := hookRSA(rsa.SignPKCS1v15)(rand.Reader, private.(*rsa.PrivateKey),
    73  		crypto.SHA256, hashed)
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  
    78  	return base64.StdEncoding.EncodeToString(signature)
    79  }
    80  
    81  // GetMD5Base64 returns a string which has been base64
    82  func GetMD5Base64(bytes []byte) (base64Value string) {
    83  	md5Ctx := md5.New()
    84  	md5Ctx.Write(bytes)
    85  	md5Value := md5Ctx.Sum(nil)
    86  	base64Value = base64.StdEncoding.EncodeToString(md5Value)
    87  	return
    88  }
    89  
    90  // GetTimeInFormatISO8601 returns a time string
    91  func GetTimeInFormatISO8601() (timeStr string) {
    92  	gmt := time.FixedZone("GMT", 0)
    93  
    94  	return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
    95  }
    96  
    97  // GetURLFormedMap returns a url encoded string
    98  func GetURLFormedMap(source map[string]string) (urlEncoded string) {
    99  	urlEncoder := url.Values{}
   100  	for key, value := range source {
   101  		urlEncoder.Add(key, value)
   102  	}
   103  	urlEncoded = urlEncoder.Encode()
   104  	return
   105  }
   106  
   107  func newUUID() uuid {
   108  	ns := uuid{}
   109  	safeRandom(ns[:])
   110  	u := newFromHash(md5.New(), ns, RandStringBytes(16))
   111  	u[6] = (u[6] & 0x0f) | (byte(2) << 4)
   112  	u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
   113  
   114  	return u
   115  }
   116  
   117  func newFromHash(h hash.Hash, ns uuid, name string) uuid {
   118  	u := uuid{}
   119  	h.Write(ns[:])
   120  	h.Write([]byte(name))
   121  	copy(u[:], h.Sum(nil))
   122  
   123  	return u
   124  }
   125  
   126  func safeRandom(dest []byte) {
   127  	if _, err := hookRead(rand.Read)(dest); err != nil {
   128  		panic(err)
   129  	}
   130  }
   131  
   132  func (u uuid) String() string {
   133  	buf := make([]byte, 36)
   134  
   135  	hex.Encode(buf[0:8], u[0:4])
   136  	buf[8] = '-'
   137  	hex.Encode(buf[9:13], u[4:6])
   138  	buf[13] = '-'
   139  	hex.Encode(buf[14:18], u[6:8])
   140  	buf[18] = '-'
   141  	hex.Encode(buf[19:23], u[8:10])
   142  	buf[23] = '-'
   143  	hex.Encode(buf[24:], u[10:])
   144  
   145  	return string(buf)
   146  }
   147  

View as plain text