经典算法实现
random
hash
是把任意长度的输入通过散列算法变换成固定长度的输出,该输出就是散列值.
简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数
有如下种类 MD5, SHA1, SHA224, SHA256
开源库openssl 可以实现。
ali-Crypto通过函数ali_hash_update或者ali_hash_digest实现。
typedef enum __hash_type_t {
HASH_NONE = 0,
SHA1 = 1,
SHA224 = 2,
SHA256 = 3,
SHA384 = 4,
SHA512 = 5,
MD5 = 6,
} ota_hash_type_t;
for (type = SHA1; type <= MD5; type++) {
result = ali_hash_init(type, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "init fail(%08x)", result);
}
result = ali_hash_update(_g_test_data, 13, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 1th fail(%08x)", result);
}
result = ali_hash_update(_g_test_data + 13, 63, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 2th fail(%08x)", result);
}
result = ali_hash_update(_g_test_data + 13 + 63, 65, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 3th fail(%08x)", result);
}
result = ali_hash_final(hash, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "final fail(%08x)", result);
result = ali_hash_digest(type, _g_test_data, TEST_DATA_SIZE, hash_all);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "digest fail(%08x)", result);
}