【问题标题】:How To Generate SHA256 and CRC32 in ios如何在ios中生成SHA256和CRC32
【发布时间】:2017-08-28 10:42:54
【问题描述】:

我正在做一个文件上传工作。我想生成 SHA256 和 CRC32 哈希。谁能帮助我如何生成这些哈希?我想让它适用于 iOS。

【问题讨论】:

  • 你用谷歌搜索了吗?

标签: iphone ios sha


【解决方案1】:

SHA256 在 CommonCrypto 中可用。 CRC32 不是哈希,它是循环冗余校验。

示例代码:

#import <CommonCrypto/CommonDigest.h>

NSData *dataIn = [@"Now is the time for all good computers to come to the aid of their masters." dataUsingEncoding:NSASCIIStringEncoding];
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];

CC_SHA256(dataIn.bytes, dataIn.length,  macOut.mutableBytes);

NSLog(@"dataIn: %@", dataIn);
NSLog(@"macOut: %@", macOut);

NSLog 输出:
dataIn:

macOut:

【讨论】:

  • 好的。但是我想获取任何数据的CRC32值。有没有办法在ios中获取它?
  • 我想为 ios 工作。在 ios 中我没有在安全框架中找到
  • 是的,Apple 正在转向转换,但我刚刚使用 iOS 5.0 SDK 对此进行了测试。至于 crc32,它在 zlib 等几个库中都有。
  • 您的 SHA256 对我有用。我还有一个问题,我正在从图像数据 (NSData) 生成哈希。是否有任何计算器/网站可以检查它?
  • 要访问 CommonCrypto 库,请确保将 iOS Security.framework 添加到您的项目中。
【解决方案2】:

对于这两个,您可以使用以下要点:

https://gist.github.com/paul-delange/6808278

还有一个例子

NSString* crc32 = (__bridge NSString*)TGDFileHashCreateWithPath((__bridge CFStringRef)filepath, TGDFileHashDefaultChunkSizeForReadingData, TGDChecksumAlgorithmCRC32);

【讨论】:

  • 太棒了。谢谢。
【解决方案3】:

此方法将从文件路径生成 iOS 上 gcloud 使用的 crc32c。如果您想要标准 crc32,只需取消注释 CRC32_POLYNOMIAL 的其他值。

它读取 512KB 块中给定的文件,因此可以用于大文件。

- (NSString*) crc32c:(NSString*)filepath{

    ///  using crc code from
    //   http://classroomm.com/objective-c/index.php?action=printpage;topic=2891.0
    //   by rgronlie


    //this is the standard crc32 polynomial
    //uint32_t CRC32_POLYNOMIAL = 0xEDB88320;

    //this is the crc32c one
    uint32_t CRC32_POLYNOMIAL = 0x82F63B78L;
    uint32_t  CRC32C_SEED = 0xFFFFFFFFL;

    // create and populate a lookup table
    uint32_t* pCRCTable = malloc(sizeof(uint32_t) * 256);

    for (uint32_t i = 0; i <= 255; i++)
    {
        uint32_t crc32 = i;
        for (uint32_t j = 8; j > 0; j--)
        {
            if ((crc32 & 1) == 1)
                crc32 = (crc32 >> 1) ^ CRC32_POLYNOMIAL;
            else
                crc32 >>= 1;
        }
        pCRCTable[i] = crc32;
    }

    // get a handle to the file
    NSFileHandle *filehandle = [NSFileHandle fileHandleForReadingAtPath:filepath];

    if(filehandle == NULL){
        NSLog(@"failed to create file handle");
        return nil;
    }

    // a buffer to read into
    NSData* databuffer;

    uint32_t crc = CRC32C_SEED;

    // read the file in chunks of 512KB

    while(true){
        databuffer = [filehandle readDataOfLength: 512 * 1024];

        // if there is nothing left finish
        if([databuffer length] == 0){
            break;
        }
        // otherwise run each chunk through the lookup table
        uint8_t *pBytes = (uint8_t *)[databuffer bytes];
        uint32_t length = [databuffer length];

        while (length--)
        {
            crc = (crc>>8) ^ pCRCTable[(crc & 0xFF) ^ *pBytes++];
        }
    }

    // clean up
    [filehandle closeFile];
    free(pCRCTable);

    // this is the result
    uint32_t hash = crc ^ 0xFFFFFFFFL;

    // reverse it for endianness
    uint32_t hash_reversed = CFSwapInt32HostToBig(hash);
    // as raw bytes
    NSData* hash_data = [NSData dataWithBytes: &hash_reversed length: sizeof(hash_reversed)];
    // return base64 encoded
    return [hash_data base64EncodedStringWithOptions:0];
}

【讨论】:

    【解决方案4】:

    没有可以为 ios 生成哈希的应用

    这应该可以工作....它适用于 Mac

    http://itunes.apple.com/us/app/digiprint/id473233587?mt=12

    【讨论】:

    • 对 Yagnesh 有所帮助的尝试很好,但我认为原始发帖人是在询问如何以编程方式为他自己的上传程序生成 SHA256 和 CRC32 数字。
    • @MichaelDautermann 是的,你是对的。你有什么建议吗??
    • @MichaelDautermann 我实现了 CC_SHA256(dataIn.bytes, dataIn.length, macOut.mutableBytes);并获取哈希 sha256 数据,但它与在线哈希生成器不匹配任何建议
    猜你喜欢
    • 2017-01-23
    • 2016-10-25
    • 2018-05-21
    • 1970-01-01
    • 2017-01-31
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多