在iPhone应用程序中实现HMAC加密算法 [英] Implementing HMAC encryption algorithm in iPhone application

查看:201
本文介绍了在iPhone应用程序中实现HMAC加密算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的iPhone应用程序实现HMAC加密算法。任何示例代码真的会有帮助。

I want to implement HMAC encryption algorithm for my iPhone application. Any sample code will really help. Also, please guide me with brief implementation of the same.

推荐答案

使用通用加密功能。 文档在手册页中,因此您需要搜索一下。它们在iOS和Mac OS X上的libSystem中,因此无需向项目中添加其他库或框架。从下面的例子可以看出,API与OpenSSL非常相似。

Use the Common Crypto functions. The documentation is in man pages, so you'll need to hunt for it a bit. They're in libSystem on iOS and Mac OS X, so no need to add another library or framework to your project. As you can see from the example below, the API is very similar to OpenSSL's.

如果你真的对加密感兴趣,而不是认证数据,Common Crypto有函数执行AES和3DES(和DES,但不使用它,它对于现代需求太弱了)。请查看 CCCryptor 手册页。

If you are actually interested in encrypting, as opposed to authenticating data, Common Crypto has functions to perform AES and 3DES (and DES, but don't use it, it's far too weak for modern needs). Take a look at the CCCryptor man page for details.

下面的例子等效于运行 openssl dgst -md5 -hmac secret< ; myfile.txt 。首先初始化CCHmacContext,然后调用CCHmacUpdate,只要有数据要进行身份验证。当您读取所有字节时,调用CCHmacFinal将HMAC转换为缓冲区。我提供了一个粗略的方法来将HMAC字节转换为可打印的十六进制。

The example below is equivalent to running openssl dgst -md5 -hmac secret < myfile.txt. Start by initializing the the CCHmacContext, and then call CCHmacUpdate as long as you have data to authenticate. When you've read all the bytes, call CCHmacFinal to get the HMAC into a buffer. I've provided a crude method to convert the HMAC bytes into printable hex.

#include <CommonCrypto/CommonHMAC.h>

#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

extern int      errno;

    int
main( int ac, char *av[] )
{
    CCHmacContext    ctx;
    char             *key = "secret";
    char             buf[ 8192 ];
    unsigned char    mac[ CC_MD5_DIGEST_LENGTH ];
    char             hexmac[ 2 * CC_MD5_DIGEST_LENGTH + 1 ];
    char             *p;
    int              fd;
    int              rr, i;

    if ( ac != 2 ) {
        fprintf( stderr, "usage: %s path\n", av[ 0 ] );
        exit( 1 );
    }

    if (( fd = open( av[ 1 ], O_RDONLY )) < 0 ) {
        fprintf( stderr, "open %s: %s\n", av[ 1 ], strerror( errno ));
        exit( 2 );
    }

    CCHmacInit( &ctx, kCCHmacAlgMD5, key, strlen( key ));

    while (( rr = read( fd, buf, sizeof( buf ))) > 0 ) {
        CCHmacUpdate( &ctx, buf, rr );
    }
    if ( rr < 0 ) {
        perror( "read" );
        exit( 2 );
    }
    CCHmacFinal( &ctx, mac );

    (void)close( fd );

    p = hexmac;
    for ( i = 0; i < CC_MD5_DIGEST_LENGTH; i++ ) {
        snprintf( p, 3, "%02x", mac[ i ] );
        p += 2;
    }

    printf( "%s\n", hexmac );

    return( 0 );
}

这篇关于在iPhone应用程序中实现HMAC加密算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆