Recently, PHP is used to implement a hashAlgorithm, The problem and solution are as follows:
1. php only supports signed integers.Signed and unsigned numbers.
A positive integer (64-bit) stores the corresponding value, and a negative number stores the corresponding value as a supplementary code.
Function strtoint ($ Str) {If (bccomp ($ STR, '000000')> = 0) {$ STR = bcsub ('20140901', $ Str ); $ STR = "-". $ STR;} else if (bccomp ($ STR, '-100') <0) {$ STR = bcadd ('20140901', $ Str);} return (INT) $ STR ;}
2. php does not support the Left shift of unsigned integers. If it is a negative number, you need to manually convert it to the corresponding unsigned number.
3. php cannot perform type conversion (I mean (Static_cast<Int>)Char *). If you want to convert a bytecode to an int, You need to convert each character to an ASCII value.
Function addfor64a ($ data) {$ sum = bcadd (string) (ord ($ data [0]), (string) (ord ($ data [1]) <8); $ sum = bcadd ($ sum, (string) (ord ($ data [2]) <16); $ sum = bcadd ($ sum, (string) (ord ($ data [3]) <24); $ sum = bcadd ($ sum, (string) (ord ($ data [4]) <32); $ sum = bcadd ($ sum, (string) (ord ($ data [5]) <40); $ sum = bcadd ($ sum, (string) (ord ($ data [6]) <48); $ sum = bcadd ($ sum, (string) (ord ($ data [7]) <56); return $ this-> strtoint ($ sum );}
After the problem is clarified, it is not difficult to calculate the hash.