@@ -8,18 +8,27 @@ namespace QBox
88{
99 public class DigestAuthClient : Client
1010 {
11- public override void SetAuth ( HttpWebRequest request )
11+ public override void SetAuth ( HttpWebRequest request , byte [ ] body )
1212 {
1313 byte [ ] secretKey = Encoding . ASCII . GetBytes ( Config . SECRET_KEY ) ;
1414 using ( HMACSHA1 hmac = new HMACSHA1 ( secretKey ) )
1515 {
1616 string pathAndQuery = request . Address . PathAndQuery ;
17- byte [ ] bytesIn = Encoding . ASCII . GetBytes ( pathAndQuery + "\n " ) ;
18- byte [ ] digest = hmac . ComputeHash ( bytesIn ) ;
19- string digestBase64 = Base64UrlSafe . Encode ( digest ) ;
17+ byte [ ] pathAndQueryBytes = Encoding . ASCII . GetBytes ( pathAndQuery ) ;
18+ using ( MemoryStream buffer = new MemoryStream ( ) )
19+ {
20+ buffer . Write ( pathAndQueryBytes , 0 , pathAndQueryBytes . Length ) ;
21+ buffer . WriteByte ( ( byte ) '\n ' ) ;
22+ if ( request . ContentType == "application/x-www-form-urlencoded" && body != null )
23+ {
24+ buffer . Write ( body , 0 , body . Length ) ;
25+ }
26+ byte [ ] digest = hmac . ComputeHash ( buffer . ToArray ( ) ) ;
27+ string digestBase64 = Base64UrlSafe . Encode ( digest ) ;
2028
21- string authHead = "QBox " + Config . ACCESS_KEY + ":" + digestBase64 ;
22- request . Headers . Add ( "Authorization" , authHead ) ;
29+ string authHead = "QBox " + Config . ACCESS_KEY + ":" + digestBase64 ;
30+ request . Headers . Add ( "Authorization" , authHead ) ;
31+ }
2332 }
2433 }
2534 }
0 commit comments