static char * build_A2_hash(heim_digest_t context, const char *method) { unsigned char md[CC_MD5_DIGEST_LENGTH]; CC_MD5_CTX ctx; char *A2; CC_MD5_Init(&ctx); if (method) CC_MD5_Update(&ctx, method, (CC_LONG)strlen(method)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, context->clientURI, (CC_LONG)strlen(context->clientURI)); /* conf|int */ if (context->type == HEIM_DIGEST_TYPE_RFC2831) { if (strcasecmp(context->clientQOP, "auth-int") == 0 || strcasecmp(context->clientQOP, "auth-conf") == 0) { /* XXX if we have a body hash, use that */ static char conf_zeros[] = ":00000000000000000000000000000000"; CC_MD5_Update(&ctx, conf_zeros, sizeof(conf_zeros) - 1); } } else { /* support auth-int ? */ if (context->clientQOP && strcasecmp(context->clientQOP, "auth") != 0) return NULL; } CC_MD5_Final(md, &ctx); hex_encode(md, sizeof(md), &A2); if (A2) strlwr(A2); return A2; }
HashResult MD5CommonCryptoImpl::Calculate(Aws::IStream& stream) { CC_MD5_CTX md5; CC_MD5_Init(&md5); auto currentPos = stream.tellg(); stream.seekg(0, stream.beg); char streamBuffer[Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE]; while(stream.good()) { stream.read(streamBuffer, Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE); auto bytesRead = stream.gcount(); if(bytesRead > 0) { CC_MD5_Update(&md5, streamBuffer, static_cast<CC_LONG>(bytesRead)); } } stream.clear(); stream.seekg(currentPos, stream.beg); ByteBuffer hash(CC_MD5_DIGEST_LENGTH); CC_MD5_Final(hash.GetUnderlyingData(), &md5); return HashResult(std::move(hash)); }
CFDataRef WebApiClientMD5DigestCreateWithFilePath(CFStringRef filePath, size_t bufferSize) { // Declare needed variables CFDataRef result = NULL; CFReadStreamRef readStream = NULL; // Get the file URL CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)filePath, kCFURLPOSIXPathStyle, (Boolean)false); if ( fileURL ) { // Create and open the read stream readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, fileURL); if ( readStream ) { Boolean didSucceed = CFReadStreamOpen(readStream); if ( didSucceed ) { // Initialize the hash object CC_MD5_CTX hashObject; CC_MD5_Init(&hashObject); // Make sure chunkSizeForReadingData is valid if ( bufferSize < 1 ) { bufferSize = FileHashDefaultChunkSizeForReadingData; } // Feed the data to the hash object bool hasMoreData = true; while ( hasMoreData ) { uint8_t buffer[bufferSize]; CFIndex readBytesCount = CFReadStreamRead(readStream, (UInt8 *)buffer, (CFIndex)sizeof(buffer)); if ( readBytesCount == -1 ) break; if ( readBytesCount == 0 ) { hasMoreData = false; continue; } CC_MD5_Update(&hashObject, (const void *)buffer, (CC_LONG)readBytesCount); } // Check if the read operation succeeded didSucceed = !hasMoreData; // Compute the hash digest unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5_Final(digest, &hashObject); // Abort if the read operation failed if ( didSucceed ) { result = CFDataCreate(kCFAllocatorDefault, digest, CC_MD5_DIGEST_LENGTH); } } } } if ( readStream ) { CFReadStreamClose(readStream); CFRelease(readStream); } if ( fileURL ) { CFRelease(fileURL); } return result; }
static boolean_t should_update_icon(FILE *current_icon) { struct stat sbuf; off_t current_icon_size; CC_MD5_CTX ctx; size_t n; unsigned char buf[1024]; unsigned char current_icon_md[CC_MD5_DIGEST_LENGTH]; int i; fstat(fileno(current_icon), &sbuf); current_icon_size = sbuf.st_size; if (current_icon_size != snowflake_icon_size) { return (B_FALSE); } CC_MD5_Init(&ctx); while ((n = fread(buf, 1, 1024, current_icon)) > 0) { CC_MD5_Update(&ctx, buf, (CC_LONG)n); } CC_MD5_Final(current_icon_md, &ctx); rewind(current_icon); for(i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { if (current_icon_md[i] != snowflake_icon_md[i]) { return (B_FALSE); } } return (B_TRUE); }
static char * build_digest(heim_digest_t context, const char *a1, const char *method) { CC_MD5_CTX ctx; uint8_t md[CC_MD5_DIGEST_LENGTH]; char *a2, *str = NULL; a2 = build_A2_hash(context, method); if (a2 == NULL) return NULL; CC_MD5_Init(&ctx); CC_MD5_Update(&ctx, a1, (CC_LONG)strlen(a1)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, context->serverNonce, (CC_LONG)strlen(context->serverNonce)); if (context->type != HEIM_DIGEST_TYPE_RFC2069) { CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, context->clientNC, (CC_LONG)strlen(context->clientNC)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, context->clientNonce, (CC_LONG)strlen(context->clientNonce)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, context->clientQOP, (CC_LONG)strlen(context->clientQOP)); } CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, a2, (CC_LONG)strlen(a2)); CC_MD5_Final(md, &ctx); free(a2); hex_encode(md, sizeof(md), &str); if (str) strlwr(str); return str; }
/*** MD5 ***/ static OSStatus HashMD5Init(SSLBuffer *digestCtx, SSLContext *sslCtx) { assert(digestCtx->length >= sizeof(CC_MD5_CTX)); CC_MD5_CTX *ctx = (CC_MD5_CTX *)digestCtx->data; CC_MD5_Init(ctx); dgprintf(("###HashMD5Init ctx %p\n", ctx)); return noErr; }
static void _wi_md5_ctx_init(_wi_md5_ctx_t *ctx) { #ifdef WI_MD5_OPENSSL MD5_Init(&ctx->openssl_ctx); #endif #ifdef WI_MD5_COMMONCRYPTO CC_MD5_Init(&ctx->commondigest_ctx); #endif }
void ocspdMD5( const void *data, CC_LONG len, unsigned char *md) // allocd by caller, CC_MD5_DIGEST_LENGTH bytes { CC_MD5_CTX ctx; CC_MD5_Init(&ctx); CC_MD5_Update(&ctx, data, len); CC_MD5_Final(md, &ctx); }
void crypt_md5_hash(const void* content, size_t content_size, void* md5, size_t md5_size) { assert(md5_size >= 16); CC_MD5_CTX context; CC_MD5_Init(&context); CC_MD5_Update(&context, content, (CC_LONG)content_size); CC_MD5_Final(md5, &context); }
static int file_compare(FILE *f1, FILE *f2) { struct stat sbuf; off_t f1size, f2size; CC_MD5_CTX ctx; size_t n; unsigned char buf[1024]; unsigned char mdresult1[CC_MD5_DIGEST_LENGTH]; unsigned char mdresult2[CC_MD5_DIGEST_LENGTH]; int i; fstat(fileno(f1), &sbuf); f1size = sbuf.st_size; fstat(fileno(f2), &sbuf); f2size = sbuf.st_size; if (f1size != f2size) return (-1); CC_MD5_Init(&ctx); while ((n = fread(buf, 1, 1024, f1)) > 0) { CC_MD5_Update(&ctx, buf, (CC_LONG)n); } CC_MD5_Final(mdresult1, &ctx); rewind(f1); CC_MD5_Init(&ctx); while ((n = fread(buf, 1, 1024, f2)) > 0) { CC_MD5_Update(&ctx, buf, (CC_LONG)n); } CC_MD5_Final(mdresult2, &ctx); rewind(f2); for(i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { if (mdresult1[i] != mdresult2[i]) return (-1); } return (0); }
static char * build_A1_hash(int type, const char *username, const char *password, const char *realm, const char *serverNonce, const char *clientNonce, const char *auth_id) { unsigned char md[CC_MD5_DIGEST_LENGTH]; CC_MD5_CTX ctx; char *A1; CC_MD5_Init(&ctx); CC_MD5_Update(&ctx, username, strlen(username)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, realm, strlen(realm)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, password, strlen(password)); CC_MD5_Final(md, &ctx); if (type != HEIM_DIGEST_TYPE_RFC2069) { CC_MD5_Init(&ctx); CC_MD5_Update(&ctx, md, sizeof(md)); memset(md, 0, sizeof(md)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, serverNonce, strlen(serverNonce)); if (clientNonce) { CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, clientNonce, strlen(clientNonce)); } if (auth_id) { CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, auth_id, strlen(auth_id)); } CC_MD5_Final(md, &ctx); } hex_encode(md, sizeof(md), &A1); if (A1) strlwr(A1); return A1; }
Crypto::Digest Crypto::DigestOpen(DigestAlgo algo) { auto d = new CCDigest(size_t(algo.get())); switch(d->algo) { case CCDigestAlgo::MD5: d->v=calloc(sizeof(CC_MD5_CTX), 1); CC_MD5_Init (static_cast<CC_MD5_CTX*> (d->v)); break; case CCDigestAlgo::SHA1: d->v=calloc(sizeof(CC_SHA1_CTX), 1); CC_SHA1_Init (static_cast<CC_SHA1_CTX*> (d->v)); break; case CCDigestAlgo::SHA256: d->v=calloc(sizeof(CC_SHA256_CTX),1); CC_SHA256_Init(static_cast<CC_SHA256_CTX*>(d->v)); break; case CCDigestAlgo::SHA384: d->v=calloc(sizeof(CC_SHA512_CTX),1); CC_SHA384_Init(static_cast<CC_SHA512_CTX*>(d->v)); break; case CCDigestAlgo::SHA512: d->v=calloc(sizeof(CC_SHA512_CTX),1); CC_SHA512_Init(static_cast<CC_SHA512_CTX*>(d->v)); break; default: d->v=0; break; } return d; }
static void md_init(int type, void *ctxp) { switch (type) { case MD_TYPE_MD5: CC_MD5_Init(ctxp); break; case MD_TYPE_SHA1: CC_SHA1_Init(ctxp); break; case MD_TYPE_SHA256: CC_SHA256_Init(ctxp); break; case MD_TYPE_SHA384: CC_SHA384_Init(ctxp); break; case MD_TYPE_SHA512: CC_SHA512_Init(ctxp); break; default: break; } }
extern "C" int32_t AppleCryptoNative_DigestFinal(DigestCtx* ctx, uint8_t* pOutput, int32_t cbOutput) { if (ctx == nullptr || pOutput == nullptr || cbOutput < ctx->cbDigest) return -1; int32_t ret = 0; switch (ctx->algorithm) { case PAL_MD5: ret = CC_MD5_Final(pOutput, &ctx->d.md5); break; case PAL_SHA1: ret = CC_SHA1_Final(pOutput, &ctx->d.sha1); break; case PAL_SHA256: ret = CC_SHA256_Final(pOutput, &ctx->d.sha256); break; case PAL_SHA384: ret = CC_SHA384_Final(pOutput, &ctx->d.sha384); break; case PAL_SHA512: ret = CC_SHA512_Final(pOutput, &ctx->d.sha512); break; default: ret = -1; break; } if (ret != 1) { return ret; } switch (ctx->algorithm) { case PAL_MD5: return CC_MD5_Init(&ctx->d.md5); case PAL_SHA1: return CC_SHA1_Init(&ctx->d.sha1); case PAL_SHA256: return CC_SHA256_Init(&ctx->d.sha256); case PAL_SHA384: return CC_SHA384_Init(&ctx->d.sha384); case PAL_SHA512: return CC_SHA512_Init(&ctx->d.sha512); default: assert(false); return -2; } }
static void digest_userhash(const char *user, const char *realm, const char *password, unsigned char md[CC_MD5_DIGEST_LENGTH]) { CC_MD5_CTX ctx; CC_MD5_Init(&ctx); CC_MD5_Update(&ctx, user, (CC_LONG)strlen(user)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, realm, (CC_LONG)strlen(realm)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, password, (CC_LONG)strlen(password)); CC_MD5_Final(md, &ctx); }
static char * build_A1_hash(heim_digest_t context) { unsigned char md[CC_MD5_DIGEST_LENGTH]; CC_MD5_CTX ctx; char *A1; if (context->flags & F_HAVE_HA1) { memcpy(md, context->SecretHash, sizeof(md)); } else if (context->flags & F_HAVE_HASH) { memcpy(md, context->SecretHash, sizeof(md)); } else if (context->password) { if (context->clientUsername == NULL) return NULL; if (context->serverRealm == NULL) return NULL; digest_userhash(context->clientUsername, context->serverRealm, context->password, md); } else return NULL; if ((context->type == HEIM_DIGEST_TYPE_RFC2617_MD5_SESS || context->type == HEIM_DIGEST_TYPE_RFC2831) && (context->flags & F_HAVE_HA1) == 0) { if (context->serverNonce == NULL) return NULL; CC_MD5_Init(&ctx); CC_MD5_Update(&ctx, md, sizeof(md)); memset(md, 0, sizeof(md)); CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, context->serverNonce, (CC_LONG)strlen(context->serverNonce)); if (context->clientNonce) { CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, context->clientNonce, (CC_LONG)strlen(context->clientNonce)); } if (context->type == HEIM_DIGEST_TYPE_RFC2831 && context->auth_id) { CC_MD5_Update(&ctx, ":", 1); CC_MD5_Update(&ctx, context->auth_id, (CC_LONG)strlen(context->auth_id)); } CC_MD5_Final(md, &ctx); } hex_encode(md, sizeof(md), &A1); if (A1) strlwr(A1); return A1; }
static krb5_error_code k5_md5_hash(unsigned int icount, const krb5_data *input, krb5_data *output) { CC_MD5_CTX ctx; unsigned int i; if (output->length != CC_MD5_DIGEST_LENGTH) return(KRB5_CRYPTO_INTERNAL); CC_MD5_Init(&ctx); for (i=0; i<icount; i++) CC_MD5_Update(&ctx, (unsigned char *) input[i].data, input[i].length); CC_MD5_Final((unsigned char *)output->data, &ctx); return(0); }
void cCCHmacFinal(CCHmacContext *ctx, void *m) { size_t i; unsigned char md5[CC_MD5_DIGEST_LENGTH]; /* 4. Compute MD5 of result of step 3 */ CC_MD5_Final(md5, &ctx->md5ctx); /* 5. XOR step 1 key with 0x5c */ for (i=0; i<CC_MD5_BLOCK_BYTES; ++i) ctx->keydata[i] ^= (0x36 ^ 0x5c); /* 6. Append MD5 result from step 4 to result of step 5 */ /* 7. Compute MD5 of result of step 6 and output the result */ CC_MD5_Init(&ctx->md5ctx); CC_MD5_Update(&ctx->md5ctx, ctx->keydata, CC_MD5_BLOCK_BYTES); CC_MD5_Update(&ctx->md5ctx, md5, CC_MD5_DIGEST_LENGTH); CC_MD5_Final(m, &ctx->md5ctx); }
extern "C" DigestCtx* AppleCryptoNative_DigestCreate(PAL_HashAlgorithm algorithm, int32_t* pcbDigest) { if (pcbDigest == nullptr) return nullptr; DigestCtx* digestCtx = reinterpret_cast<DigestCtx*>(malloc(sizeof(DigestCtx))); if (digestCtx == nullptr) return nullptr; digestCtx->algorithm = algorithm; switch (algorithm) { case PAL_MD5: *pcbDigest = CC_MD5_DIGEST_LENGTH; CC_MD5_Init(&digestCtx->d.md5); break; case PAL_SHA1: *pcbDigest = CC_SHA1_DIGEST_LENGTH; CC_SHA1_Init(&digestCtx->d.sha1); break; case PAL_SHA256: *pcbDigest = CC_SHA256_DIGEST_LENGTH; CC_SHA256_Init(&digestCtx->d.sha256); break; case PAL_SHA384: *pcbDigest = CC_SHA384_DIGEST_LENGTH; CC_SHA384_Init(&digestCtx->d.sha384); break; case PAL_SHA512: *pcbDigest = CC_SHA512_DIGEST_LENGTH; CC_SHA512_Init(&digestCtx->d.sha512); break; default: *pcbDigest = -1; free(digestCtx); return nullptr; } digestCtx->cbDigest = *pcbDigest; return digestCtx; }
void * SecCmsUtilGetHashObjByAlgID(SECAlgorithmID *algid) { SECOidData *oidData = SECOID_FindOID(&(algid->algorithm)); if (oidData) { void *digobj = NULL; switch (oidData->offset) { case SEC_OID_SHA1: digobj = calloc(1, sizeof(CC_SHA1_CTX)); CC_SHA1_Init(digobj); break; case SEC_OID_MD5: digobj = calloc(1, sizeof(CC_MD5_CTX)); CC_MD5_Init(digobj); break; case SEC_OID_SHA224: digobj = calloc(1, sizeof(CC_SHA256_CTX)); CC_SHA224_Init(digobj); break; case SEC_OID_SHA256: digobj = calloc(1, sizeof(CC_SHA256_CTX)); CC_SHA256_Init(digobj); break; case SEC_OID_SHA384: digobj = calloc(1, sizeof(CC_SHA512_CTX)); CC_SHA384_Init(digobj); break; case SEC_OID_SHA512: digobj = calloc(1, sizeof(CC_SHA512_CTX)); CC_SHA512_Init(digobj); break; default: break; } return digobj; } return 0; }
void cCCHmacInit(CCHmacContext *ctx, int alg, const void *k, size_t kl) { size_t i; if (alg != kCCHmacAlgMD5) die("cCCHmacInit requires kCCHmacAlgMD5"); /* 1. Get CC_MD5_BLOCK_BYTES byte key */ if (kl > CC_MD5_BLOCK_BYTES) kl = CC_MD5_BLOCK_BYTES; memcpy(ctx->keydata, k, kl); if (kl < CC_MD5_BLOCK_BYTES) memset(&ctx->keydata[kl], 0, CC_MD5_BLOCK_BYTES - kl); /* 2. XOR step 1 key with 0x36 */ for (i=0; i<CC_MD5_BLOCK_BYTES; ++i) ctx->keydata[i] ^= 0x36; /* 3. Append text to the result of step 2 */ /* 4. Compute MD5 of result of step 3 */ CC_MD5_Init(&ctx->md5ctx); CC_MD5_Update(&ctx->md5ctx, ctx->keydata, CC_MD5_BLOCK_BYTES); }
/*** *** MD5 ***/ void MD5Object::digestInit() { mIsDone = false; CC_MD5_Init(&mCtx); }
/* * Compute a HMAC MD5 sum. * Taken from rfc2104, Appendix. */ static void signature_compute_hmac_md5(const u_int8_t *text, int text_len, unsigned char *key, unsigned int key_len, u_int8_t *digest) { CC_MD5_CTX context; unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */ unsigned char k_opad[65]; /* outer padding - key XORd with opad */ unsigned char tk[16]; int i; /* if key is longer than 64 bytes reset it to key=MD5(key) */ if (key_len > 64) { CC_MD5_CTX tctx; CC_MD5_Init(&tctx); CC_MD5_Update(&tctx, key, key_len); CC_MD5_Final(tk, &tctx); key = tk; key_len = 16; } /* * the HMAC_MD5 transform looks like: * * MD5(K XOR opad, MD5(K XOR ipad, text)) * * where K is an n byte key * ipad is the byte 0x36 repeated 64 times * opad is the byte 0x5c repeated 64 times * and text is the data being protected */ /* start out by storing key in pads */ memset(k_ipad, 0, sizeof k_ipad); memset(k_opad, 0, sizeof k_opad); memcpy(k_ipad, key, key_len); memcpy(k_opad, key, key_len); /* XOR key with ipad and opad values */ for (i=0; i<64; i++) { k_ipad[i] ^= 0x36; k_opad[i] ^= 0x5c; } /* * perform inner MD5 */ CC_MD5_Init(&context); /* init context for 1st pass */ CC_MD5_Update(&context, k_ipad, 64); /* start with inner pad */ CC_MD5_Update(&context, text, text_len); /* then text of datagram */ CC_MD5_Final(digest, &context); /* finish up 1st pass */ /* * perform outer MD5 */ CC_MD5_Init(&context); /* init context for 2nd pass */ CC_MD5_Update(&context, k_opad, 64); /* start with outer pad */ CC_MD5_Update(&context, digest, 16); /* then results of 1st hash */ CC_MD5_Final(digest, &context); /* finish up 2nd pass */ }
CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath, size_t chunkSizeForReadingData) { CFStringRef result = NULL; CFReadStreamRef readStream = NULL; CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)filePath, kCFURLPOSIXPathStyle, (Boolean) false); if (!fileURL) { goto done; } readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, (CFURLRef)fileURL); if (!readStream) { goto done; } bool didSucceed = (bool)CFReadStreamOpen(readStream); if (!didSucceed) { goto done; } // Initialize the hash object CC_MD5_CTX hashObject; CC_MD5_Init(&hashObject); // Make sure the chunkSizeForReadindData is valid if (!chunkSizeForReadingData) { chunkSizeForReadingData = FileHashDefaultChunksSizeForReadingData; } // Feed the data to the hash object bool hasMoreData = true; while (hasMoreData) { uint8_t buffer[chunkSizeForReadingData]; CFIndex readBytesCount = CFReadStreamRead(readStream, (UInt8 *)buffer, (CFIndex)sizeof(buffer)); if (readBytesCount == -1) { break; } if (readBytesCount == 0) { hasMoreData = false; continue; } CC_MD5_Update(&hashObject, (const void *)buffer, (CC_LONG)readBytesCount); } // Check if the read operation has succeded didSucceed = !hasMoreData; // Compute the hash digest unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5_Final(digest, &hashObject); // Abort if the read operation fails if (!didSucceed) { goto done; } // Compute the string result char hash[2 * sizeof(digest) + 1]; for (size_t i = 0; i < sizeof(digest); ++i) { snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i])); } result = CFStringCreateWithCString(kCFAllocatorDefault, (const char *)hash, kCFStringEncodingUTF8); done: if (readStream) { CFReadStreamClose(readStream); CFRelease(readStream); } if (fileURL) { CFRelease(fileURL); } return result; }
C4Err HASH_Init(HASH_Algorithm algorithm, HASH_ContextRef * ctx) { int err = kC4Err_NoErr; HASH_Context* hashCTX = NULL; const struct ltc_hash_descriptor* desc = NULL; ValidateParam(ctx); *ctx = NULL; hashCTX = XMALLOC(sizeof (HASH_Context)); CKNULL(hashCTX); hashCTX->magic = kHASH_ContextMagic; hashCTX->algor = algorithm; #if _USES_COMMON_CRYPTO_ switch(algorithm) { case kHASH_Algorithm_MD5: hashCTX->ccAlgor = kCCHmacAlgMD5; hashCTX->process = (void*) sCCHashUpdateMD5; hashCTX->done = (void*) sCCHashFinalMD5; hashCTX->hashsize = 16; CC_MD5_Init(&hashCTX->state.ccMD5_state); break; case kHASH_Algorithm_SHA1: hashCTX->ccAlgor = kCCHmacAlgSHA1; hashCTX->hashsize = 20; hashCTX->process = (void*) sCCHashUpdateSHA1; hashCTX->done = (void*) sCCHashFinalSHA1;; CC_SHA1_Init(&hashCTX->state.ccSHA1_state); break; case kHASH_Algorithm_SHA224: hashCTX->ccAlgor = kCCHmacAlgSHA224; hashCTX->hashsize = 28; hashCTX->process = (void*) sCCHashUpdateSHA224; hashCTX->done = (void*) sCCHashFinalSHA224; CC_SHA224_Init(&hashCTX->state.ccSHA256_state); break; case kHASH_Algorithm_SHA256: hashCTX->ccAlgor = kCCHmacAlgSHA256; hashCTX->hashsize = 32; hashCTX->process = (void*) sCCHashUpdateSHA256; hashCTX->done = (void*) sCCHashFinalSHA256;; CC_SHA256_Init(&hashCTX->state.ccSHA256_state); break; case kHASH_Algorithm_SHA384: hashCTX->ccAlgor = kCCHmacAlgSHA384; hashCTX->hashsize = 48; hashCTX->process = (void*) sCCHashUpdateSHA384; hashCTX->done = (void*) sCCHashFinalSHA384; CC_SHA384_Init(&hashCTX->state.ccSHA512_state); break; case kHASH_Algorithm_SHA512: hashCTX->ccAlgor = kCCHmacAlgSHA512; hashCTX->hashsize = 64; hashCTX->process = (void*) sCCHashUpdateSHA512; hashCTX->done = (void*) sCCHashFinalSHA512; CC_SHA512_Init(&hashCTX->state.ccSHA512_state); break; default: hashCTX->ccAlgor = kCCHmacAlgInvalid; break; } if(hashCTX->ccAlgor == kCCHmacAlgInvalid) { desc = sDescriptorForHash(algorithm); hashCTX->hashsize = desc->hashsize; hashCTX->process = (void*) desc->process; hashCTX->done = (void*) desc->done; if(IsNull(desc)) RETERR( kC4Err_BadHashNumber); if(desc->init) err = (desc->init)(&hashCTX->state.tc_state); CKERR; } #else desc = sDescriptorForHash(algorithm); hashCTX->hashsize = desc->hashsize; hashCTX->process = (void*) desc->process; hashCTX->done = (void*) desc->done; if(IsNull(desc)) RETERR( kC4Err_BadHashNumber); if(desc->init) err = (desc->init)(&hashCTX->state.tc_state); CKERR; #endif *ctx = hashCTX; done: if(IsC4Err(err)) { if(IsntNull(hashCTX)) { XFREE(hashCTX); } } return err; }
CFStringRef GCCreateMD5HashWithPath(CFStringRef filePath) { CFStringRef result = NULL; CFReadStreamRef readStream = NULL; CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)filePath, kCFURLPOSIXPathStyle, (Boolean)false); if (!fileURL) goto done; readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, (CFURLRef)fileURL); if (!readStream) goto done; bool didSucceed = (bool)CFReadStreamOpen(readStream); if (!didSucceed) goto done; CC_MD5_CTX hashObject; CC_MD5_Init(&hashObject); bool hasMoreData = true; while (hasMoreData) { uint8_t buffer[GCFileChunkSize]; CFIndex readBytesCount = CFReadStreamRead(readStream, (UInt8 *)buffer, (CFIndex)sizeof(buffer)); if (readBytesCount == -1) break; if (readBytesCount == 0) { hasMoreData = false; continue; } CC_MD5_Update(&hashObject, (const void *)buffer, (CC_LONG)readBytesCount); } didSucceed = !hasMoreData; unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5_Final(digest, &hashObject); if (!didSucceed) goto done; char hash[2 * sizeof(digest) + 1]; for (size_t i = 0; i < sizeof(digest); ++i) { snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i])); } result = CFStringCreateWithCString(kCFAllocatorDefault, (const char *)hash, kCFStringEncodingUTF8); done: if (readStream) { CFReadStreamClose(readStream); CFRelease(readStream); } if (fileURL) { CFRelease(fileURL); } return result; }
static int __archive_libsystem_md5init(archive_md5_ctx *ctx) { CC_MD5_Init(ctx); return (ARCHIVE_OK); }
MD5Digest::MD5Digest() : Digest(kSecDigestMD5, CC_MD5_DIGEST_LENGTH) { CC_MD5_Init(&mContext); }