Beispiel #1
0
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_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 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;
}
void MD5Object::digestFinal(
	void 		*digest)
{
	if(mIsDone) {
		throw std::runtime_error("MD5 digestFinal after final");
	}
	CC_MD5_Final((unsigned char *)digest, &mCtx);
	mIsDone = true;
}
Beispiel #7
0
static void _wi_md5_ctx_final(unsigned char *buffer, _wi_md5_ctx_t *ctx) {
#ifdef WI_MD5_OPENSSL
    MD5_Final(buffer, &ctx->openssl_ctx);
#endif
    
#ifdef WI_MD5_COMMONCRYPTO
    CC_MD5_Final(buffer, &ctx->commondigest_ctx);
#endif
}
Beispiel #8
0
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);
    
}
Beispiel #9
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);
}
Beispiel #10
0
int sCCHashFinalMD5(void *ctx, unsigned char *out)
{
    CC_MD5_Final(out, ctx);
    
#ifdef LTC_CLEAN_STACK
    zeromem(ctx, sizeof(CC_SHA1_CTX));
#endif
    
    return CRYPT_OK;
}
Beispiel #11
0
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);
}
Beispiel #12
0
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);
}
Beispiel #13
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;
}
Beispiel #14
0
static void
md_final(int type, unsigned char *str, void *ctx)
{
  switch (type) {
  case MD_TYPE_MD5:	CC_MD5_Final(str, ctx);		break;
  case MD_TYPE_SHA1:	CC_SHA1_Final(str, ctx);	break;
  case MD_TYPE_SHA256:	CC_SHA256_Final(str, ctx);	break;
  case MD_TYPE_SHA384:	CC_SHA384_Final(str, ctx);	break;
  case MD_TYPE_SHA512:	CC_SHA512_Final(str, ctx);	break;
  default:						break;
  }
}
Beispiel #15
0
static OSStatus HashMD5Final(SSLBuffer *digestCtx, SSLBuffer *digest)
{
	assert(digestCtx->length >= sizeof(CC_MD5_CTX));
	CC_MD5_CTX *ctx = (CC_MD5_CTX *)digestCtx->data;
	dgprintf(("###HashMD5Final  ctx %p\n", ctx));
	assert(digest->length >= CC_MD5_DIGEST_LENGTH);
	//if (digest->length < CC_MD5_DIGEST_LENGTH)
	//	return errSSLCrypto;
	CC_MD5_Final(digest->data, ctx);
	digest->length = CC_MD5_DIGEST_LENGTH;
    return noErr;
}
Beispiel #16
0
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);
}
string Crypto::DigestFinish(Digest x) {
  auto d = FromVoid<CCDigest*>(x);
  string ret;
  switch(d->algo) {
    case CCDigestAlgo::MD5:    ret.resize(CC_MD5_DIGEST_LENGTH);    CC_MD5_Final   (MakeUnsigned(&ret[0]), static_cast<CC_MD5_CTX*>   (d->v)); free(d->v); d->v=0; break;
    case CCDigestAlgo::SHA1:   ret.resize(CC_SHA1_DIGEST_LENGTH);   CC_SHA1_Final  (MakeUnsigned(&ret[0]), static_cast<CC_SHA1_CTX*>  (d->v)); free(d->v); d->v=0; break;
    case CCDigestAlgo::SHA256: ret.resize(CC_SHA256_DIGEST_LENGTH); CC_SHA256_Final(MakeUnsigned(&ret[0]), static_cast<CC_SHA256_CTX*>(d->v)); free(d->v); d->v=0; break;
    case CCDigestAlgo::SHA384: ret.resize(CC_SHA384_DIGEST_LENGTH); CC_SHA384_Final(MakeUnsigned(&ret[0]), static_cast<CC_SHA512_CTX*>(d->v)); free(d->v); d->v=0; break;
    case CCDigestAlgo::SHA512: ret.resize(CC_SHA512_DIGEST_LENGTH); CC_SHA512_Final(MakeUnsigned(&ret[0]), static_cast<CC_SHA512_CTX*>(d->v)); free(d->v); d->v=0; break;
    default: break;
  }
  delete d;
  return ret;
}
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;
}
Beispiel #20
0
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);
}
Beispiel #21
0
/*
 * 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 */
}
Beispiel #22
0
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;
}
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;
}
Beispiel #24
0
static int
__archive_libsystem_md5final(archive_md5_ctx *ctx, void *md)
{
  CC_MD5_Final(md, ctx);
  return (ARCHIVE_OK);
}
const void* MD5Digest::Finalize()
{
	CC_MD5_Final(mDigestBuffer, &mContext);
	return mDigestBuffer;
}