前面说过了tars中的tc_md5.h/tc_md5.cpp, 对外呈现的信息很简单, 就是md5, 而tc_sha.h/tc_sha.cpp也是一样, 无非就是各种sha的api而已。
看一下部分sha源码, 就知道没有继续看的必要了。 它们虽然复杂, 却很简单:
/* SHA256 mixing function definitions */ #define s256_0(x) (rotr32_sha2((x), 2) ^ rotr32_sha2((x), 13) ^ rotr32_sha2((x), 22)) #define s256_1(x) (rotr32_sha2((x), 6) ^ rotr32_sha2((x), 11) ^ rotr32_sha2((x), 25)) #define g256_0(x) (rotr32_sha2((x), 7) ^ rotr32_sha2((x), 18) ^ ((x) >> 3)) #define g256_1(x) (rotr32_sha2((x), 17) ^ rotr32_sha2((x), 19) ^ ((x) >> 10)) /* rotated SHA256 round definition. Rather than swapping variables as in */ /* FIPS-180, different variables are 'rotated' on each round, returning */ /* to their starting positions every eight rounds */ #define h2(i) ctx->wbuf[i & 15] += \ g256_1(ctx->wbuf[(i + 14) & 15]) + ctx->wbuf[(i + 9) & 15] + g256_0(ctx->wbuf[(i + 1) & 15]) #define h2_cycle(i,j) \ v[(7 - i) & 7] += (j ? h2(i) : ctx->wbuf[i & 15]) + k256[i + j] \ + s256_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \ v[(3 - i) & 7] += v[(7 - i) & 7]; \ v[(7 - i) & 7] += s256_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7]) /* SHA256 mixing data */ const uint32_t k256[64] = { n_u32(428a2f98), n_u32(71374491), n_u32(b5c0fbcf), n_u32(e9b5dba5), n_u32(3956c25b), n_u32(59f111f1), n_u32(923f82a4), n_u32(ab1c5ed5), n_u32(d807aa98), n_u32(12835b01), n_u32(243185be), n_u32(550c7dc3), n_u32(72be5d74), n_u32(80deb1fe), n_u32(9bdc06a7), n_u32(c19bf174), n_u32(e49b69c1), n_u32(efbe4786), n_u32(0fc19dc6), n_u32(240ca1cc), n_u32(2de92c6f), n_u32(4a7484aa), n_u32(5cb0a9dc), n_u32(76f988da), n_u32(983e5152), n_u32(a831c66d), n_u32(b00327c8), n_u32(bf597fc7), n_u32(c6e00bf3), n_u32(d5a79147), n_u32(06ca6351), n_u32(14292967), n_u32(27b70a85), n_u32(2e1b2138), n_u32(4d2c6dfc), n_u32(53380d13), n_u32(650a7354), n_u32(766a0abb), n_u32(81c2c92e), n_u32(92722c85), n_u32(a2bfe8a1), n_u32(a81a664b), n_u32(c24b8b70), n_u32(c76c51a3), n_u32(d192e819), n_u32(d6990624), n_u32(f40e3585), n_u32(106aa070), n_u32(19a4c116), n_u32(1e376c08), n_u32(2748774c), n_u32(34b0bcb5), n_u32(391c0cb3), n_u32(4ed8aa4a), n_u32(5b9cca4f), n_u32(682e6ff3), n_u32(748f82ee), n_u32(78a5636f), n_u32(84c87814), n_u32(8cc70208), n_u32(90befffa), n_u32(a4506ceb), n_u32(bef9a3f7), n_u32(c67178f2), }; /* SHA256 initialisation data */ const uint32_t i256[8] = { n_u32(6a09e667), n_u32(bb67ae85), n_u32(3c6ef372), n_u32(a54ff53a), n_u32(510e527f), n_u32(9b05688c), n_u32(1f83d9ab), n_u32(5be0cd19) }; void sha256_begin(sha256_ctx ctx[1]) { ctx->count[0] = ctx->count[1] = 0; memcpy(ctx->hash, i256, 8 * sizeof(uint32_t)); }