// must include Security framework to use this
// Tiger or later for SHA256


#import "libCdsaCrypt.h"

@implementation NSData (DataDigest)

- (NSData *) digestWithCDSAType:(CSSM_ALGORITHMS) algorithm
{ // does not validate algorithm selection, let the framework worry about that.
        NSData * result = nil;
        int error = noErr;
        CSSM_DATA inData;
        CSSM_CSP_HANDLE cspHandle;
        CSSM_DATA digestData;

        error = cdsaCspAttach(&cspHandle);
        if(!error)
                {
                inData.Data = (void *)[self bytes];
                inData.Length = [self length];
                /* calculate digest */
                error = cdsaDigest(cspHandle, algorithm, &inData, &digestData);
                if(!error)
                        {
result = [NSData dataWithBytesNoCopy: digestData.Data length: digestData.Length freeWhenDone: YES];
                        }
                }
        return result;
}

- (NSData *) md5Digest
{
        return [self digestWithCDSAType: CSSM_ALGID_MD5];
}

- (NSData *) sha1Digest
{
        return [self digestWithCDSAType: CSSM_ALGID_SHA1];
}

#ifndef CSSM_ALGID_SHA256
#define CSSM_ALGID_SHA256 0
#endif

- (NSData *) sha256Digest
{
return [self digestWithCDSAType: CSSM_ALGID_SHA256];
}



@end

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to