clock()函数的一个坑

clock_t s = clock();

Sleep(5*1000);

printf("%d\n", clock() - s);

这段代码在Windows上大于或等于5秒,但是在Linux上就小于5秒。

查资料发现 标准的应该是 Returns the processor time consumed by the program. 返回程序消耗的处理器时间。

但在MSDN上 Calculates the wall-clock time used by the calling process. 计算调用进程使用的挂钟时间,

注释写的是  The clock function tells how much wall-clock time has passed since the CRT initialization during process start. Note that this function does not strictly conform to ISO C, which specifies net CPU time as the return value. To obtain CPU times, use the Win32 GetProcessTimes function. To determine the elapsed time in seconds, divide the value returned by the clock function by the macro CLOCKS_PER_SEC. 

在Linux上是占用CPU时间,在Windows上是运行时间。所以跨平台的时候就要注意了,不要使用这个函数来做计时。

参考 http://www.51testing.com/html/00/n-3706800.html

OPENSSL AES解密文件遇到的一个小坑

    openssl aes解密需要用到 EVP_CIPHER_CTX_new EVP_DecryptInit_ex EVP_DecryptUpdate EVP_DecryptFinal_ex EVP_CIPHER_CTX_free 五个函数。

默认启用了填充,如果不需要需调用EVP_CIPHER_CTX_set_padding设成0禁用。

在解密大文件的时候需要持续调用EVP_DecryptUpdate来解密,我犯了个错误就是inbuf大小和outbuf大小设成一样,出问题查官方文档才知道outbuf大小要比inbuf大。

EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex() are the corresponding decryption operations. EVP_DecryptFinal() will return an error code if padding is enabled and the final block is not correctly formatted. The parameters and restrictions are identical to the encryption operations except that if padding is enabled the decrypted data buffer out passed to EVP_DecryptUpdate() should have sufficient room for (inl + cipher_block_size) bytes unless the cipher block size is 1 in which case inl bytes is sufficient.

网上的代码不能乱抄啊!看官方文档的重要性。

https://www.openssl.org/docs/man1.0.2/crypto/EVP_DecryptUpdate.html

https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption