Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

TEC-007 java and php SHA-256 signature and extension

Shulou Source: shulou.com Published: 2022-06-03 07:27:58 09月11日 Update

Due to business requirements, the php project uses SHA-256 signature and extension to connect a java api. Together with java students, we study the SHA-256 signature and extension of php and java, and share the operations:

Java demo is as follows:

Public class SignUtils {

Public static String toSign (String data, String salt) {

String s = data + salt

Byte [] sign

Try {

Sign = SHA256.Digest.getInstance ("SHA-256") .digest (s.getBytes (Charset.forName ("UTF-8")

Byte [] signBase = Base64.getEncoder () .encode (sign)

Return new String (signBase,Charset.forName ("UTF-8"))

} catch (Exception e) {

Throw new Exception ("signature exception!")

}

}

}

String text = "lwy"

String salt = "lwy20190803"

String signData = SignUtils.toSign (text, salt)

System.ount.println (signData)

Output: QXulYxjtlJ1e/hEaeTKSjnSY8Xf37GuLRwAsnkEWN94=

Php demo is as follows:

Class SignUtils {

Public static function toSign ($data,$salt) {

$string = $data.$salt

$data = hash ('sha256', $string)

Return base64_encode (hex2bin ($data))

}

}

$sign = new SignUtils ()

$salt = "lwy20190803"

$str = "lwy"

$signData = $sign- > toSign ($str,$salt)

Echo "signature string:", $str.$salt

Echo "signature result:", $signData

Output result:

Signature string: lwylwy20190803

Signature result: QXulYxjtlJ1e/hEaeTKSjnSY8Xf37GuLRwAsnkEWN94=

This demo thanks Brother Abo for helping with the debugging step by step.

Tags: Results UTF-8 output Business students Big Brothers requirements projects Research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology Huawei Shulou Information macOS