Monday, 7 November 2022

PHP Convert Hex String to String

How to decode Hex String to String?

Ex: Decode Hex String to get JWT token when integrating Apple ID login


Solved

Hex string is a data type commonly used for transmission over the internet in the lower network layers. In some cases PHP will need to manipulate this data type directly. To handle them, PHP provides us with the "pack" function to manipulate Hex and binary string data.

Ex:


$stringHexData = "65794a68624763694f694a49557a49314e694973496e523563434936496b705856434a392e65794a7063334d694f694a6f64485277637a6f764c3246776347786c61575175595842776247557559323974496977695958566b496a6f69593239744c6e4e68625842735a533568634841694c434a6c654841694f6a45324d5459354f4455794e6a4973496d6c68644349364d5459784e4449314d444d334d69776963335669496a6f694d4441774e7a677a4c6d4e6b5a6a45354d47526b4d544130595452685a6a42694e6d49774e6a63784d3249344d4455304d6a52694c6a45304d5451694c434a6a58326868633267694f694a7852557035546d7473556d4a5a626c46565679316b646d6774536b7452496977695a573168615777694f694a7a59573177624756415a3231686157777559323974496977695a57316861577866646d567961575a705a5751694f694a30636e566c4969776959585630614639306157316c496a6f784e6a45324f5441774d7a63794c434a756232356a5a56397a6458427762334a305a5751694f6e527964575573496e4a6c5957786664584e6c636c397a6447463064584d694f6a4a392e764b6c394873303057587463426851796f47326742766a744375394d5878787471674e46777854772d5034";

$stringData = pack("H*",$stringHexData);

echo $stringData;

Result (JWT String): 


eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLnNhbXBsZS5hcHAiLCJleHAiOjE2MTY5ODUyNjIsImlhdCI6MTYxNDI1MDM3Miwic3ViIjoiMDAwNzgzLmNkZjE5MGRkMTA0YTRhZjBiNmIwNjcxM2I4MDU0MjRiLjE0MTQiLCJjX2hhc2giOiJxRUp5TmtsUmJZblFVVy1kdmgtSktRIiwiZW1haWwiOiJzYW1wbGVAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOiJ0cnVlIiwiYXV0aF90aW1lIjoxNjE2OTAwMzcyLCJub25jZV9zdXBwb3J0ZWQiOnRydWUsInJlYWxfdXNlcl9zdGF0dXMiOjJ9.vKl9Hs00WXtcBhQyoG2gBvjtCu9MXxxtqgNFwxTw-P4

More 

In addition, the "pack" function also supports other data types. 


possible values of format are:
a – string which is NUL-padded
A – string which is SPACE-padded
h – low nibble first Hex string
H – high nibble first Hex string
c – signed character
C – unsigned character
s – signed short (16 bit, machine byte order)
S – unsigned short ( 16 bit, machine byte order)
n – unsigned short ( 16 bit, big endian byte order)
v – unsigned short ( 16 bit, little endian byte order)
i – signed integer (machine dependent byte order and size)
I – unsigned integer (machine dependent byte order and size)
l – signed long ( 32 bit, machine byte order)
L – unsigned long ( 32 bit, machine byte order)
N – unsigned long ( 32 bit, big endian byte order)
V – unsigned long ( 32 bit, little endian byte order)
f – float (machine dependent representation and size)
d – double (machine dependent representation and size)
x – NUL byte
X – Back up one byte
Z – string which is NUL-padded
@ – NUL-fill to absolute position

0 nhận xét:

Post a Comment