github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/baidupcs/netdisksign/locatedownloadsign.go (about)

     1  package netdisksign
     2  
     3  import (
     4  	"crypto/sha1"
     5  	"encoding/hex"
     6  	"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/cachepool"
     7  	"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/converter"
     8  	"strconv"
     9  	"time"
    10  )
    11  
    12  type (
    13  	LocateDownloadSign struct {
    14  		Time   int64
    15  		Rand   string
    16  		DevUID string
    17  	}
    18  )
    19  
    20  func NewLocateDownloadSign(uid uint64, bduss string) *LocateDownloadSign {
    21  	return NewLocateDownloadSignWithTimeAndDevUID(time.Now().Unix(), DevUID(bduss), uid, bduss)
    22  }
    23  
    24  func NewLocateDownloadSignWithTimeAndDevUID(timeunix int64, devuid string, uid uint64, bduss string) *LocateDownloadSign {
    25  	l := &LocateDownloadSign{
    26  		Time:   timeunix,
    27  		DevUID: devuid,
    28  	}
    29  	l.Sign(uid, bduss)
    30  	return l
    31  }
    32  
    33  func (s *LocateDownloadSign) Sign(uid uint64, bduss string) {
    34  	randSha1 := sha1.New()
    35  	bdussSha1 := sha1.New()
    36  	bdussSha1.Write(converter.ToBytes(bduss))
    37  	sha1ResHex := cachepool.RawMallocByteSlice(40)
    38  	hex.Encode(sha1ResHex, bdussSha1.Sum(nil))
    39  	randSha1.Write(sha1ResHex)
    40  	uidStr := strconv.FormatUint(uid, 10)
    41  	randSha1.Write(converter.ToBytes(uidStr))
    42  	randSha1.Write([]byte{'\x65', '\x62', '\x72', '\x63', '\x55', '\x59', '\x69', '\x75', '\x78', '\x61', '\x5a', '\x76', '\x32', '\x58', '\x47', '\x75', '\x37', '\x4b', '\x49', '\x59', '\x4b', '\x78', '\x55', '\x72', '\x71', '\x66', '\x6e', '\x4f', '\x66', '\x70', '\x44', '\x46'})
    43  	timeStr := strconv.FormatInt(s.Time, 10)
    44  	randSha1.Write(converter.ToBytes(timeStr))
    45  	randSha1.Write(converter.ToBytes(s.DevUID))
    46  	hex.Encode(sha1ResHex, randSha1.Sum(nil))
    47  	s.Rand = converter.ToString(sha1ResHex)
    48  }
    49  
    50  func (s *LocateDownloadSign) URLParam() string {
    51  	return "time=" + strconv.FormatInt(s.Time, 10) + "&rand=" + s.Rand + "&devuid=" + s.DevUID + "&cuid=" + s.DevUID
    52  }