github.com/coreservice-io/utils@v0.3.0/example/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/coreservice-io/utils/bytes_util"
     9  	"github.com/coreservice-io/utils/hash_util"
    10  	"github.com/coreservice-io/utils/rand_util"
    11  	"github.com/coreservice-io/utils/time_util"
    12  	"github.com/coreservice-io/utils/token_util"
    13  )
    14  
    15  func main() {
    16  	//time_util
    17  	log.Println(time_util.GetUTCDate())
    18  	log.Println(time_util.GetUTCDateTime())
    19  
    20  	//bytes_util
    21  	log.Println(bytes_util.Format(12312341344))
    22  	bytes_num, err := bytes_util.Parse("11.47TB") //case insensitive
    23  	if err != nil {
    24  		log.Println(err.Error())
    25  	} else {
    26  		log.Println(bytes_num)
    27  	}
    28  
    29  	//rand_util
    30  	log.Println("randstr:" + rand_util.GenRandStr(80))
    31  
    32  	//hash_util
    33  	log.Println(hash_util.MD5Hash([]byte("123123")))
    34  	log.Println(hash_util.MD5HashString("123123"))
    35  
    36  	log.Println(hash_util.SHA256([]byte("123123")))
    37  	log.Println(hash_util.SHA256String("123123"))
    38  
    39  	log.Println(hash_util.CRC32HashString("123123"))
    40  	log.Println(hash_util.CRC32HashString("123123"))
    41  	log.Println(hash_util.CRC32HashArrayString([]string{"1", "2", "3", "1", "2", "3"}))
    42  	log.Println(hash_util.CRC32HashArrayString([]string{"1", "2", "3", "1", "2", "3"}))
    43  
    44  	//token util
    45  	time_now := time.Now().Unix()
    46  	tutil := token_util.NewTokenUtil("test salt")
    47  	fmt.Println("new token util pass secs:", time.Now().Unix()-time_now)
    48  	//
    49  	tutil.AppendWhiteListToken(map[string]interface{}{"token_w": struct{}{}})
    50  	//
    51  
    52  	log.Println("token_util instance:", tutil)
    53  	token1 := tutil.GenToken()
    54  	token2 := tutil.GenToken()
    55  	token3 := tutil.GenSuperToken()
    56  
    57  	token1_, token1_s := tutil.CheckToken(token1)
    58  	log.Println("check token1 is token:", token1_, " is super token:", token1_s)
    59  
    60  	token2_, token2_s := tutil.CheckToken(token2)
    61  	log.Println("check token2 is token:", token2_, " is super token:", token2_s)
    62  
    63  	token3_, token3_s := tutil.CheckToken(token3)
    64  	log.Println("check token3 is token:", token3_, " is super token:", token3_s)
    65  
    66  	tokenw_, tokenw_s := tutil.CheckToken("token_w")
    67  	log.Println("check tokenw is token:", tokenw_, " is super token:", tokenw_s)
    68  
    69  	tokene_, tokene_s := tutil.CheckToken("token_err")
    70  	log.Println("check token_err is token:", tokene_, " is super token:", tokene_s)
    71  
    72  	log.Println("get white list token:", tutil.GetWhiteListToken("token_w"))
    73  	log.Println("get white list token:", tutil.GetWhiteListToken("token_err"))
    74  
    75  }