The source code is this:
public class Authorizationsignature {public static string createsignature (string verb, string contentMD5, String con Tenttype, String date, string canonicalizedsalheaders, String canonicalizedresource) { string signaturestr = Verb + "\\n" + contentMD5 + "\\n" + contentType + "\\n" + date + "\\n" + canonicalizedsalheaders + canonicalizedresour CE; Signaturestr = Java.net.URLDecoder.decode (SIGNATURESTR); Hmacsha1signature HSS = new Hmacsha1signature (); String tosignature = Hss.computesignature (Authconfiguration.getinstance (). Getauthconfigurationdto () . Getsecretkey (), signaturestr); String authorization = authconfiguration.getinstance (). Getauthconfigurationdto (). Getenterpriseheader () + Authconfiguration.getinstance (). Getauthconfigurationdto (). Getaccesskey () + ":" + tosignature; return authorization;} }
public class Hmacsha1signature extends Servicesignature {private static final Logger LOG = Loggerfactory.getlogger (Hma Csha1signature.class); private static final String Default_charset = "UTF-8"; Private static final String algorithm = "HmacSHA1"; @Override public String Getalgorithm () {return algorithm; } @Override public string computesignature (string key, String data) {byte[] signdata = null; try {signdata = signature (Key.getbytes (default_charset), Data.getbytes (Default_charset)); } catch (Unsupportedencodingexception ex) {Log.debug (Ex.getmessage ()); } return Binaryutil.tobase64string (SignData); } private byte[] signature (byte[] key, byte[] data) {try {mac Mac = mac.getinstance (algorithm); Mac.init (New Secretkeyspec (key, algorithm)); return mac.dofinal (data); } catch (NoSuchAlgorithmException E1) {//throw new RuntimeexceptioN ("Unsupported Algorithm:hmacsha1"); Log.error ("Unsupported Algorithm:hmacsha1", E1); return null; } catch (InvalidKeyException e) {//throw new RuntimeException (); Log.debug (E.getmessage ()); return null; } }}
public class Binaryutil { private static final Logger LOG = Loggerfactory.getlogger (binaryutil.class); public static string ToBase64String (byte[] binarydata) { string tobase64result = null; try { Tobase64result = new String (Base64.encodebase64 (binarydata), contentutil.byte_code); } catch ( Unsupportedencodingexception e) { log.debug (e.getmessage ()); } return tobase64result; } public static byte[] FromBase64String (String base64string) { byte[] frombase64result = null; try { Frombase64result = base64.decodebase64 (Base64string.getbytes (Contentutil.byte_code)); } catch ( Unsupportedencodingexception e) { log.debug (e.getmessage ()); } return frombase64result;} }
Need to rewrite to PHP code, step-by-step analysis, the first is the core of the MAC_SHA1 signature algorithm
Mac Mac = mac.getinstance (algorithm); Mac.init (New Secretkeyspec (key, algorithm)); return mac.dofinal (data);
The equivalent PHP code is called the built-in function Hash_hmac, the Java code is to return a byte array for the byte array signature, so here the return value needs to be processed, processed into a byte array
<?phprequire_once Dir_sal. ' util/bytesutil.php '; require_once dir_sal. ' util/base64util.php '; class Hmacsha1signature {public Function computesignature ($key, $data) {$hash = $this->hmac ($ Data, $key), $hash = Str_split ($hash), foreach ($hash as $index + = $value) {$asc = ord ($value); if ($asc > 128 {$hash [$index] = Ord ($value)-128 * 2;} else {$hash [$index] = ord ($value);}} $bytes = Base64util::encodebase64 ($hash); return Bytesutil::tostr ($bytes);} Private Function HMAC ($data, $key, $hashFunc = ' SHA1 ', $rawOutput = True) {if (! In_array ($hashFunc, Hash_algos ())) {$ Hashfunc = ' SHA1 ';} Return Hash_hmac ($hashFunc, $data, $key, $rawOutput);}}
The base64 conversion of the resulting byte array is also performed in the Java code
public static string ToBase64String (byte[] binarydata) { string tobase64result = null; try { Tobase64result = new String (Base64.encodebase64 (binarydata), contentutil.byte_code); } catch ( Unsupportedencodingexception e) { log.debug (e.getmessage ()); } return tobase64result; }
This transcoding method conforms to the definition of Base64, the 3 8-bit representation of the data into 4 6-bit identification data, that is, 3*8=4*6, which will be more than a few byte values, the specific algorithm implemented through the Bing found in the 2 articles in the Java Code Integrated implementation ( Direct use of base64 for string encoding results and expected mismatch):
<?phpclass base64util {public static function EncodeBase64 ($data) {$encodes = " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; require_once DIR_VENTOR. ' sal/util/bytesutil.php '; $encodes = Bytesutil::getbytes_10 ($encodes); $dataLength = Intval (count ($data)); $modulus = Intval ($dataLength% 3);//The result should be the number of digits if ($modulus = = 0) {//byte bits can be divisible by 3 $sblength = Intval ((4 * $dataLength)/3);} els e {$sbLength = Intval (4 * (Intval ($dataLength/3) + 1));} $SB = Array (); $pos = 0; $val = 0;foreach ($data as $i + = $byte) {$val = ($val << 8) | ($data [$i] & 0xFF), $pos + = 8;while ($pos > 5) {$index = $val >> ($pos-= 6); $SB [] = $encodes [$index]; $va L &= ((1 << $pos)-1);}} if ($pos > 0) {$index = $val << (6-$pos); $SB [] = $encodes [$index];} The number of digits is not sufficient with the = character (ASCII value 61) to populate $real = count ($SB), if ($real < $sbLength) {for ($i = 0; $i < $sbLength-$real; $i + +) {$s b [] = 61;}} return $SB;}}
PHP rewrite of a Java signature algorithm