数据转换-位串字节数组
1 参考《GMT 0009-2012 SM2密码算法使用规范》第6节“数据转换” 在utils.h和utils.c中完成位串与8位字节串的转换功能
2 并写出测试代码测试上述函数
3 提交代码(或代码链接)和运行结果
1 #include "sdf.h" 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main(){ 6 7 void ** pdh; 8 pdh = (void **) malloc(20); 9 int ret; 10 11 ret = SDF_OpenDevice(pdh); 12 if(ret != SDR_OK){ 13 printf("error!"); 14 } else { 15 printf("device opened!\n"); 16 } 17 18 DEVICEINFO testdi; 19 ret = SDF_GetDeviceInfo(pdh, &testdi); 20 if(ret != SDR_OK){ 21 printf("error!"); 22 } else { 23 printf("Issuer Name: %s\n", testdi.IssuerName); 24 printf("Device Name: %s\n", testdi.DeviceName); 25 printf("Device Serial: %s\n", testdi.DeviceSerial); 26 printf("Device Version: %d\n", testdi.DeviceVersion); 27 28 } 29 30 char pRandom[10]; 31 32 ret = SDF_GenerateRandom(*pdh,10, pRandom); 33 if(ret != SDR_OK){ 34 printf("error!"); 35 } else { 36 for(int i=0; i<10; i++) 37 printf("%d\n", pRandom[i]); 38 } 39 40 41 ret = SDF_CloseDevice(*pdh); 42 43 if(ret != SDR_OK){ 44 printf("error!"); 45 } else { 46 free(pdh); 47 printf("device closed!\n"); 48 } 49 50 return 0; 51 }
1 #include "sdf.h" 2 #include <string.h> 3 #include <time.h> 4 #include <stdlib.h> 5 6 //******************************** 7 //设备管理 8 //******************************** 9 10 int SDF_OpenDevice(void ** phDeviceHandle){ 11 return SDR_OK; 12 } 13 14 15 int SDF_CloseDevice(void *hDeviceHandle){ 16 17 return SDR_OK; 18 } 19 20 int SDF_GetDeviceInfo( void * hSessionHandle, DEVICEINFO * pstDeviceInfo) { 21 22 DEVICEINFO di; 23 strcpy(di.IssuerName,"RocSDF"); 24 strcpy(di.DeviceName,"SDFBESTI181x"); 25 strcpy(di.DeviceSerial,"2021040001"); 26 di.DeviceVersion = 1; 27 //... 28 29 //pstDevicelnfo = &di; 30 *pstDeviceInfo = di; 31 32 return SDR_OK; 33 } 34 35 static int myRandom(){ 36 srand((unsigned)time(NULL)); 37 return rand(); 38 } 39 40 int SDF_GenerateRandom (void * hSessionHandle, unsigned int uiLength, unsigned char * pucRandom){ 41 42 43 for(int i=0; i<uiLength; i++){ 44 sleep(2); 45 *(pucRandom+i) = myRandom(); 46 // pucRandom[i] = i; 47 } 48 49 return SDR_OK; 50 }
1 #include <stdio.h> 2 #include "util.h" 3 4 char HStr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 5 char Hex2Char(int i){ 6 /* 7 if(i>=0 && i<= 9) 8 return i + 0x30; 9 //return i + '0' 10 if(i>=10 && i<=15) 11 return i + 0x37; 12 // return i + 'A' - 10; 13 */ 14 return HStr[i]; 15 }