github.com/AntonOrnatskyi/goproxy@v0.0.0-20190205095733-4526a9fa18b4/core/dst/cookie.go (about)

     1  // Copyright 2014 The DST Authors. All rights reserved.
     2  // Use of this source code is governed by an MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dst
     6  
     7  import (
     8  	"crypto/rand"
     9  	"crypto/sha256"
    10  	"encoding/binary"
    11  	"net"
    12  )
    13  
    14  var cookieKey = make([]byte, 16)
    15  
    16  func init() {
    17  	_, err := rand.Reader.Read(cookieKey)
    18  	if err != nil {
    19  		panic(err)
    20  	}
    21  }
    22  
    23  func cookie(remote net.Addr) uint32 {
    24  	hash := sha256.New()
    25  	hash.Write([]byte(remote.String()))
    26  	hash.Write(cookieKey)
    27  	bs := hash.Sum(nil)
    28  	return binary.BigEndian.Uint32(bs)
    29  }