Recently learned WP8.1 application development, want to put C language SM3 algorithm transplanted into mobile app. The conversion of C code to C # code is much more work, so it can only be implemented with the WINODWS runtime component.
SM3 state secret algorithm is a hash algorithm, the specific details please self-Baidu.
Results The results of the SM3 algorithm found on the mobile phone are different from the results of the computer running! After a little debugging I found that the displacement operation caused by the disaster, the code has the following macro definition:
#define SHL (X,n) (((x) & 0xFFFFFFFF) << N) #define ROTL (X,n) (SHL ((x), n) | ((x) >> (32-n)))
Macro definition of the first line when N is greater than 32 o'clock, the result of the mobile phone operation is different from the computer; the second line of macro definition when 32-n is less than 0 o'clock, the results of the phone operation are different from the computer.
For better testing, I decided to build a clean test environment. Start with a new C # language blank Windows Phone app project, put a TextBlock and 2 buttons in XAML
<Grid> <StackPanel> <TextBlockx:name= "Txbresult"/> <Buttonx:name= "Btncsharpleft"Content= "C # offset"Click= "Btncsharpleft_click"></Button> <Buttonx:name= "Btncleft"Content= "C Left Shift"Click= "Btncleft_click"></Button> </StackPanel> </Grid>
Implement button click events again
Private voidBtncsharpleft_click (Objectsender, RoutedEventArgs e) {Txbresult.text+="C # offset \ n"; for(inti =0; I < the; i + =8) { stringstr ="0x12345678<<"+ i.tostring () +"="+ (0x12345678<< i). ToString ("X"); Txbresult.text+ = str. PadRight ( +) +"0x12345678>>"+ i.tostring () +"="+ (0x12345678>> i). ToString ("X") +"\ n"; } } Private voidBtncleft_click (Objectsender, RoutedEventArgs e) {Txbresult.text+="C displacement \ n"; Class1 C=NewClass1 (); for(inti =0; I < the; i + =8) { stringstr ="0x12345678<<"+ i.tostring () +"="+ C.FUNC2 (0x12345678, i). ToString ("X"); Txbresult.text+ = str. PadRight ( +) +"0x12345678>>"+ i.tostring () +"="+ C.FUNC1 (0x12345678, i). ToString ("X") +"\ n"; } }
Then create a new C + + language Windows Runtime component, providing two functions for left and right displacement
int int int b) { return n >>int int int b) { Return n << b;}
And then even on the phone to test, my cell phone is nokia920, the company testing Department of the test machine, the first to use, Roar roar!
You can see that C # displacement at more than 32 o'clock is 32 modulo, that is, the displacement of 33 bits equivalent to the displacement of 1 bits, and the mobile phone running results test more than 32 bits are 0!
To determine if the C + + language's displacements are handled this way, I'll create a new WIN32 console project to test
#include"stdio.h"intMain () {printf ("c move left \ n"); for(inti =0; I < the; i + =8) {printf ("0x12345678<<%d =%x\n"I0x12345678<<i); } printf ("c move right \ n"); for(inti =0; I < the; i + =8) {printf ("0x12345678>>%d =%x\n"I0x12345678>>i); } return 0;}
The operation results are as follows
You can see that the results are the same as in C #. Indicates that only the Windows runtime components of C + + will have different results.
Similarly I test the displacement negative digits, such as 0x12345678>>-2, in C # and C + + WIN32 program displacement-2 bits equivalent to 30 bits of displacement, while in the Windows Runtime component of C + + the result is 0.
Conclusion: For compatibility, the displacement of the 32 to take the mold, such as 1>>n to write 1>> (n%32), so in different platforms can also get the same results.
Differences in WINODWS run-time component displacement operations for C + + in WP8.1