@@ -253,53 +253,28 @@ func genUserBuf(account string, dwSdkappid int, dwAuthID uint32,
253253 return userBuf
254254}
255255
256- func hmacsha256 (sdkappid int , key string , identifier string , currTime int64 , expire int , base64UserBuf * string ) string {
257- var contentToBeSigned string
258- contentToBeSigned = "TLS.identifier:" + identifier + "\n "
259- contentToBeSigned += "TLS.sdkappid:" + strconv .Itoa (sdkappid ) + "\n "
260- contentToBeSigned += "TLS.time:" + strconv .FormatInt (currTime , 10 ) + "\n "
261- contentToBeSigned += "TLS.expire:" + strconv .Itoa (expire ) + "\n "
262- if nil != base64UserBuf {
263- contentToBeSigned += "TLS.userbuf:" + * base64UserBuf + "\n "
264- }
265-
266- h := hmac .New (sha256 .New , []byte (key ))
267- h .Write ([]byte (contentToBeSigned ))
268- return base64 .StdEncoding .EncodeToString (h .Sum (nil ))
269- }
270-
271256func genSig (sdkappid int , key string , identifier string , expire int , userbuf []byte ) (string , error ) {
272257 currTime := time .Now ().Unix ()
273- sigDoc := make (map [string ]interface {})
274- sigDoc ["TLS.ver" ] = "2.0"
275- sigDoc ["TLS.identifier" ] = identifier
276- sigDoc ["TLS.sdkappid" ] = sdkappid
277- sigDoc ["TLS.expire" ] = expire
278- sigDoc ["TLS.time" ] = currTime
279- var base64UserBuf string
280- if nil != userbuf {
281- base64UserBuf = base64 .StdEncoding .EncodeToString (userbuf )
282- sigDoc ["TLS.userbuf" ] = base64UserBuf
283- sigDoc ["TLS.sig" ] = hmacsha256 (sdkappid , key , identifier , currTime , expire , & base64UserBuf )
284- } else {
285- sigDoc ["TLS.sig" ] = hmacsha256 (sdkappid , key , identifier , currTime , expire , nil )
286- }
287-
288- data , err := json .Marshal (sigDoc )
289- if err != nil {
290- return "" , err
258+ sigDoc := userSig {
259+ Version : "2.0" ,
260+ Identifier : identifier ,
261+ SdkAppID : uint64 (sdkappid ),
262+ Expire : int64 (expire ),
263+ Time : currTime ,
264+ UserBuf : userbuf ,
291265 }
266+ sigDoc .Sig = sigDoc .sign (key )
292267
293268 var b bytes.Buffer
294269 w := newZlibWriter (& b )
295270 defer zlibWriterPool .Put (w )
296- if _ , err = w . Write ( data ); err != nil {
271+ if err := json . NewEncoder ( w ). Encode ( sigDoc ); err != nil {
297272 return "" , err
298273 }
299- if err = w .Close (); err != nil {
274+ if err : = w .Close (); err != nil {
300275 return "" , err
301276 }
302- return base64urlEncode (b .Bytes ()), nil
277+ return base64url . EncodeToString (b .Bytes ()), nil
303278}
304279
305280// VerifyUserSig 检验UserSig在now时间点时是否有效
@@ -329,7 +304,7 @@ type userSig struct {
329304 Expire int64 `json:"TLS.expire,omitempty"`
330305 Time int64 `json:"TLS.time,omitempty"`
331306 UserBuf []byte `json:"TLS.userbuf,omitempty"`
332- Sig string `json:"TLS.sig,omitempty"`
307+ Sig [] byte `json:"TLS.sig,omitempty"`
333308}
334309
335310func newUserSig (usersig string ) (userSig , error ) {
@@ -375,35 +350,41 @@ func (u userSig) verify(sdkappid uint64, key string, userid string, now time.Tim
375350 } else if u .UserBuf != nil {
376351 return ErrUserBufTypeNotMatch
377352 }
378- if u .sign (key ) != u .Sig {
353+ if ! bytes . Equal ( u .sign (key ), u .Sig ) {
379354 return ErrSigNotMatch
380355 }
381356 return nil
382357}
383358
384- func (u userSig ) sign (key string ) string {
385- var sb bytes.Buffer
386- sb .WriteString ("TLS.identifier:" )
387- sb .WriteString (u .Identifier )
388- sb .WriteString ("\n " )
389- sb .WriteString ("TLS.sdkappid:" )
390- sb .WriteString (strconv .FormatUint (u .SdkAppID , 10 ))
391- sb .WriteString ("\n " )
392- sb .WriteString ("TLS.time:" )
393- sb .WriteString (strconv .FormatInt (u .Time , 10 ))
394- sb .WriteString ("\n " )
395- sb .WriteString ("TLS.expire:" )
396- sb .WriteString (strconv .FormatInt (u .Expire , 10 ))
397- sb .WriteString ("\n " )
398- if u .UserBuf != nil {
399- sb .WriteString ("TLS.userbuf:" )
400- sb .WriteString (base64 .StdEncoding .EncodeToString (u .UserBuf ))
401- sb .WriteString ("\n " )
402- }
359+ var (
360+ sigIdentifier = []byte ("TLS.identifier:" )
361+ sigSdkAppID = []byte ("TLS.sdkappid:" )
362+ sigTime = []byte ("TLS.time:" )
363+ sigExpire = []byte ("TLS.expire:" )
364+ sigUserBuf = []byte ("TLS.userbuf:" )
365+ sigEnter = []byte ("\n " )
366+ )
403367
368+ func (u userSig ) sign (key string ) []byte {
404369 h := hmac .New (sha256 .New , []byte (key ))
405- h .Write (sb .Bytes ())
406- return base64 .StdEncoding .EncodeToString (h .Sum (nil ))
370+ h .Write (sigIdentifier )
371+ h .Write ([]byte (u .Identifier ))
372+ h .Write (sigEnter )
373+ h .Write (sigSdkAppID )
374+ h .Write ([]byte (strconv .FormatUint (u .SdkAppID , 10 )))
375+ h .Write (sigEnter )
376+ h .Write (sigTime )
377+ h .Write ([]byte (strconv .FormatInt (u .Time , 10 )))
378+ h .Write (sigEnter )
379+ h .Write (sigExpire )
380+ h .Write ([]byte (strconv .FormatInt (u .Expire , 10 )))
381+ h .Write (sigEnter )
382+ if u .UserBuf != nil {
383+ h .Write (sigUserBuf )
384+ h .Write ([]byte (base64 .StdEncoding .EncodeToString (u .UserBuf )))
385+ h .Write (sigEnter )
386+ }
387+ return h .Sum (nil )
407388}
408389
409390// 错误类型
0 commit comments