forked from qjfoidnh/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
locatedownloadsign.go
52 lines (46 loc) · 1.63 KB
/
locatedownloadsign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package netdisksign
import (
"crypto/sha1"
"encoding/hex"
"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/cachepool"
"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/converter"
"strconv"
"time"
)
type (
LocateDownloadSign struct {
Time int64
Rand string
DevUID string
}
)
func NewLocateDownloadSign(uid uint64, bduss string) *LocateDownloadSign {
return NewLocateDownloadSignWithTimeAndDevUID(time.Now().Unix(), DevUID(bduss), uid, bduss)
}
func NewLocateDownloadSignWithTimeAndDevUID(timeunix int64, devuid string, uid uint64, bduss string) *LocateDownloadSign {
l := &LocateDownloadSign{
Time: timeunix,
DevUID: devuid,
}
l.Sign(uid, bduss)
return l
}
func (s *LocateDownloadSign) Sign(uid uint64, bduss string) {
randSha1 := sha1.New()
bdussSha1 := sha1.New()
bdussSha1.Write(converter.ToBytes(bduss))
sha1ResHex := cachepool.RawMallocByteSlice(40)
hex.Encode(sha1ResHex, bdussSha1.Sum(nil))
randSha1.Write(sha1ResHex)
uidStr := strconv.FormatUint(uid, 10)
randSha1.Write(converter.ToBytes(uidStr))
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'})
timeStr := strconv.FormatInt(s.Time, 10)
randSha1.Write(converter.ToBytes(timeStr))
randSha1.Write(converter.ToBytes(s.DevUID))
hex.Encode(sha1ResHex, randSha1.Sum(nil))
s.Rand = converter.ToString(sha1ResHex)
}
func (s *LocateDownloadSign) URLParam() string {
return "time=" + strconv.FormatInt(s.Time, 10) + "&rand=" + s.Rand + "&devuid=" + s.DevUID
}