Objective C相当于Java中的MessageDigest?

时间:2022-07-21 08:43:21

I'm taking a task converting Java code to Objective C.

我正在执行将Java代码转换为Objective C的任务。

This is the code in Java that I have to convert:

这是我必须转换的Java代码:

private String getHash(String input) 
{
    String ret = null;
    try 
    {
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        byte[] bs = md.digest(input.getBytes("US-ASCII"));


        StringBuffer sb = new StringBuffer();
        for (byte b : bs) 
        {
            String bt = Integer.toHexString(b & 0xff);
            if(bt.length()==1) 
            {
                sb.append("0");
            }
            sb.append(bt);
        }
        ret = sb.toString();
    } 
    catch (Exception e) 
    {
    }
    return ret;
}

Specifically, what can I use in Objective C which has the same functionality as the MessageDigest class?

具体来说,我可以在Objective C中使用哪些功能与MessageDigest类具有相同的功能?

3 个解决方案

#1


3  

Something like this:

像这样的东西:

#import <CommonCrypto/CommonDigest.h>

+(NSString*) sha256:(NSString *)input
{
    const char *s=[input cStringUsingEncoding:NSASCIIStringEncoding];
    NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];

    uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
    CC_SHA256(keyData.bytes, keyData.length, digest);
    NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
    NSString *hash=[out description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
    return hash;
}

#2


2  

I found a apple framework to support SHA-256 in *.com. Thx * :)

我在*.com找到了支持SHA-256的苹果框架。 thx * :)

CommonCrypto/CommonDigest.h

and I realized that I can use this function:

我意识到我可以使用这个功能:

CC_SHA256(const void *data, CC_LONG len, unsigned char *md)

CC_SHA256_Final(unsigned char *md, CC_SHA256_CTX *c)

CC_SHA256_Init(CC_SHA256_CTX *c)

CC_SHA256_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len)

So I can go on my task except this Java code.

所以我可以继续我的任务,除了这个Java代码。

byte[] bs = md.digest(input.getBytes("US-ASCII"));

and I want to know that any Objective C expression of Java circular code below?

我想知道Java循环代码的任何Objective C表达式如下?

for (byte b : bs)

PS : Chuck, I really appreciate for your help. Thank you. :)

PS:查克,我真的很感谢你的帮助。谢谢。 :)

#3


0  

You want the OpenSSL library. See the answers to generate sha256 with openssl and C++ for an example (the title says C++, but OpenSSL is just a basic C library).

你想要OpenSSL库。有关示例,请参阅使用openssl和C ++生成sha256的答案(标题为C ++,但OpenSSL只是一个基本的C库)。

#1


3  

Something like this:

像这样的东西:

#import <CommonCrypto/CommonDigest.h>

+(NSString*) sha256:(NSString *)input
{
    const char *s=[input cStringUsingEncoding:NSASCIIStringEncoding];
    NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];

    uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
    CC_SHA256(keyData.bytes, keyData.length, digest);
    NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
    NSString *hash=[out description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
    return hash;
}

#2


2  

I found a apple framework to support SHA-256 in *.com. Thx * :)

我在*.com找到了支持SHA-256的苹果框架。 thx * :)

CommonCrypto/CommonDigest.h

and I realized that I can use this function:

我意识到我可以使用这个功能:

CC_SHA256(const void *data, CC_LONG len, unsigned char *md)

CC_SHA256_Final(unsigned char *md, CC_SHA256_CTX *c)

CC_SHA256_Init(CC_SHA256_CTX *c)

CC_SHA256_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len)

So I can go on my task except this Java code.

所以我可以继续我的任务,除了这个Java代码。

byte[] bs = md.digest(input.getBytes("US-ASCII"));

and I want to know that any Objective C expression of Java circular code below?

我想知道Java循环代码的任何Objective C表达式如下?

for (byte b : bs)

PS : Chuck, I really appreciate for your help. Thank you. :)

PS:查克,我真的很感谢你的帮助。谢谢。 :)

#3


0  

You want the OpenSSL library. See the answers to generate sha256 with openssl and C++ for an example (the title says C++, but OpenSSL is just a basic C library).

你想要OpenSSL库。有关示例,请参阅使用openssl和C ++生成sha256的答案(标题为C ++,但OpenSSL只是一个基本的C库)。