์๋๋ ํด์ฌ ๊ฐ์ ๊ฐ์ ธ์ค๋ ์์ค์ด๋ค
pAlg ์ ์ฌ์ฉ๋๋ ๊ฐ์ md5, sha1, sha224, sha256, sm3 ๋ฑ ๋ฌธ์์ด ์ด๋ค.
pSrc ๊ฐ ์
๋ ฅ ๊ฐ
pHash ๊ฐ ๊ฒฐ๊ณผ ํด์ฌ ๊ฐ
BIN ๊ตฌ์กฐ์ฒด
typedef struct _BIN {
int nLen;
unsigned char *pVal;
} BIN;
int JS_PKI_genHash(const char * pAlg, const BIN * pSrc, BIN * pHash)
{
int nRet = 0;
EVP_MD_CTX *pCTX = NULL;
const EVP_MD *pMD = NULL;
unsigned char *pDigest = NULL;
int nDigestLen = 0;
pMD = EVP_get_digestbyname(pAlg);
if (pMD == NULL)
{
fprintf(stderr, "invalid digest algorithm(%s)\n", pAlg);
return -1;
}
pCTX = EVP_MD_CTX_create();
if (pCTX == NULL)
{
fprintf(stderr, "fail to create ctx\n");
return -2;
}
nRet = EVP_DigestInit(pCTX, pMD);
if (nRet != 1)
{
fprintf(stderr, "digest init fail(%d)\n", nRet);
nRet = -3;
goto end;
}
nRet = EVP_DigestUpdate(pCTX, pSrc->pVal, pSrc->nLen);
if (nRet != 1)
{
fprintf(stderr, "digest update fail(%d)\n", nRet);
nRet = -4;
goto end;
}
pDigest = (unsigned char *)JS_calloc(1, EVP_MD_size(pMD));
if (pDigest == NULL)
{
fprintf(stderr, "out of memeory for digest\n");
nRet = -5;
goto end;
}
nRet = EVP_DigestFinal(pCTX, pDigest, (unsigned int *)&nDigestLen);
if (nRet != 1)
{
fprintf(stderr, "digest final fail(%d)\n", nRet);
nRet = -6;
goto end;
}
if (nDigestLen > 0)
{
JS_BIN_set(pHash, pDigest, nDigestLen);
nRet = 0;
}
end :
if (pCTX)
{
EVP_MD_CTX_destroy(pCTX);
}
if (pDigest) JS_free(pDigest);
return nRet;
}
์๋๋ BerEditor ์์ ํด์ฌ ์ ํจ์๋ฅผ ๋๋ฆฐ ํ๋ฉด์ด๋ค
'Development' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
QT์์ MAC OS ์ฑ dmg ๋ง๋ค๊ธฐ (0) | 2023.03.19 |
---|---|
Mac์ฉ ํจํค์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ๋ฐฉ๋ฒ (0) | 2023.03.18 |
How to add new object id in openssl library. (0) | 2017.07.17 |
[LDAP] This is ldif data of country code. (0) | 2017.03.15 |
QT ๋ฐ ๊ฐ๋ฐ ๊ด๋ จ ๋งํฌ (0) | 2017.01.26 |