| 254 | |
| 255 | |
| 256 | bool sha256(const BYTE *data, int datalen, BYTE *sum) |
| 257 | { |
| 258 | EVP_MD_CTX *mdctx = NULL; |
| 259 | bool ret = true; |
| 260 | |
| 261 | try { |
| 262 | |
| 263 | if (EVP_MD_size(EVP_sha256()) != 32) |
| 264 | handleErrors(); |
| 265 | |
| 266 | if ((mdctx = EVP_MD_CTX_create()) == NULL) |
| 267 | handleErrors(); |
| 268 | |
| 269 | if (1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) |
| 270 | handleErrors(); |
| 271 | |
| 272 | if (1 != EVP_DigestUpdate(mdctx, data, datalen)) |
| 273 | handleErrors(); |
| 274 | |
| 275 | unsigned int len; |
| 276 | if (1 != EVP_DigestFinal_ex(mdctx, sum, &len)) |
| 277 | handleErrors(); |
| 278 | |
| 279 | if (len != 32) |
| 280 | handleErrors(); |
| 281 | |
| 282 | } catch (...) { |
| 283 | ret = false; |
| 284 | } |
| 285 | |
| 286 | if (mdctx) |
| 287 | EVP_MD_CTX_destroy(mdctx); |
| 288 | |
| 289 | return ret; |
| 290 | |
| 291 | } |
| 292 | |
| 293 | bool sha256(const string& str, BYTE* sum) |
| 294 | { |
no test coverage detected