11package com .tencentyun ;
22
3- // 使用旧版本 base64 编解码实现增强兼容性
4-
5- import sun .misc .BASE64Encoder ;
6-
7- import java .io .UnsupportedEncodingException ;
3+ import java .nio .charset .StandardCharsets ;
4+ import java .util .Base64 ;
85import java .security .*;
9- import java .nio .charset .Charset ;
106
117import java .util .Arrays ;
128import java .util .zip .Deflater ;
@@ -109,17 +105,13 @@ private String hmacsha256(String identifier, long currTime, long expire, String
109105 contentToBeSigned += "TLS.userbuf:" + base64Userbuf + "\n " ;
110106 }
111107 try {
112- byte [] byteKey = key .getBytes ("UTF-8" );
108+ byte [] byteKey = key .getBytes (StandardCharsets . UTF_8 );
113109 Mac hmac = Mac .getInstance ("HmacSHA256" );
114110 SecretKeySpec keySpec = new SecretKeySpec (byteKey , "HmacSHA256" );
115111 hmac .init (keySpec );
116- byte [] byteSig = hmac .doFinal (contentToBeSigned .getBytes ("UTF-8" ));
117- return (new BASE64Encoder ().encode (byteSig )).replaceAll ("\\ s*" , "" );
118- } catch (UnsupportedEncodingException e ) {
119- return "" ;
120- } catch (NoSuchAlgorithmException e ) {
121- return "" ;
122- } catch (InvalidKeyException e ) {
112+ byte [] byteSig = hmac .doFinal (contentToBeSigned .getBytes (StandardCharsets .UTF_8 ));
113+ return (Base64 .getEncoder ().encodeToString (byteSig )).replaceAll ("\\ s*" , "" );
114+ } catch (NoSuchAlgorithmException | InvalidKeyException e ) {
123115 return "" ;
124116 }
125117 }
@@ -137,7 +129,7 @@ private String genUserSig(String userid, long expire, byte[] userbuf) {
137129
138130 String base64UserBuf = null ;
139131 if (null != userbuf ) {
140- base64UserBuf = new BASE64Encoder ().encode (userbuf );
132+ base64UserBuf = Base64 . getEncoder ().encodeToString (userbuf ). replaceAll ( " \\ s*" , "" );
141133 sigDoc .put ("TLS.userbuf" , base64UserBuf );
142134 }
143135 String sig = hmacsha256 (userid , currTime , expire , base64UserBuf );
@@ -146,7 +138,7 @@ private String genUserSig(String userid, long expire, byte[] userbuf) {
146138 }
147139 sigDoc .put ("TLS.sig" , sig );
148140 Deflater compressor = new Deflater ();
149- compressor .setInput (sigDoc .toString ().getBytes (Charset . forName ( "UTF-8" ) ));
141+ compressor .setInput (sigDoc .toString ().getBytes (StandardCharsets . UTF_8 ));
150142 compressor .finish ();
151143 byte [] compressedBytes = new byte [2048 ];
152144 int compressedBytesLength = compressor .deflate (compressedBytes );
0 commit comments