C语言 目标 C:SHA1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3468268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:09:41  来源:igfitidea点击:

Objective C: SHA1

cobjective-chashsha1

提问by Daniel

How do i sha1 a string or set of numbers in Objective c?

我如何在 Objective c 中 sha1 一个字符串或一组数字?

回答by Jonathan Grynspan

CommonCrypto (an Apple framework) has functions for calculating SHA-1 hashes, including a one-step hash:

CommonCrypto(Apple 框架)具有计算 SHA-1 哈希的函数,包括一步哈希:

#include <CommonCrypto/CommonDigest.h>

unsigned char digest[CC_SHA1_DIGEST_LENGTH];
NSData *stringBytes = [someString dataUsingEncoding: NSUTF8StringEncoding]; /* or some other encoding */
if (CC_SHA1([stringBytes bytes], [stringBytes length], digest)) {
    /* SHA-1 hash has been calculated and stored in 'digest'. */
    ...
}

For a set of numbers, let us assume you mean an array of ints of known length. For such data, it is easier to iteratively construct the digest rather than use the one-shot function:

对于一组数字,让我们假设您指的是已知长度的整数数组。对于此类数据,迭代构建摘要比使用一次性函数更容易:

unsigned char digest[CC_SHA1_DIGEST_LENGTH];
uint32_t *someIntegers = ...;
size_t numIntegers = ...;

CC_SHA1_CTX ctx;
CC_SHA1_Init(&ctx);
{
    for (size_t i = 0; i < numIntegers; i++)
        CC_SHA1_Update(&ctx, someIntegers + i, sizeof(uint32_t));
}
CC_SHA1_Final(digest, &ctx);

/* SHA-1 hash has been calculated and stored in 'digest'. */
...

Note that this does not take endianness into account. The SHA-1 calculated with this code on a PowerPC system will differ from the one calculated on an i386 or ARM system. The solution is simple--swap the bytes of the integers to a known endianness before doing the calculation:

请注意,这不考虑字节序。在 PowerPC 系统上使用此代码计算的 SHA-1 将不同于在 i386 或 ARM 系统上计算的 SHA-1。解决方案很简单——在进行计算之前将整数的字节交换为已知的字节序:

    for (size_t i = 0; i < numIntegers; i++) {
        uint32_t swapped = CFSwapInt32HostToLittle(someIntegers[i]); /* or HostToBig */
        CC_SHA1_Update(&ctx, &swapped, sizeof(swapped));
    }

回答by Takahiko Kawasaki

Another solution with a message digest library (nv-ios-digest):

另一个带有消息摘要库 (nv-ios-digest) 的解决方案:

(1) String

(1) 字符串

// Create an SHA1 instance, update it with a string and do final.
SHA1 sha1 = [SHA1 sha1WithString:@"Hello"];

// Get the pointer of the internal buffer that holds the message digest value.
// The life of the internal buffer ends when the SHA1 instance is discarded.
// Copy the buffer as necessary. The size of the buffer can be obtained by
// 'bufferSize' method.
unsigned char *digestAsBytes = [sha1 buffer];

// Get the string expression of the message digest value.
NSString *digestAsString = [sha1 description];

(2) Numbers

(2) 数字

// Create an SHA1 instance.
SHA1 sha1 = [[SHA1 alloc] init];

// Update the SHA1 instance with numbers.
// (Sorry, the current implementation is endianness-dependent.)
[sha1 updateWithShort:(short)1];
[sha1 updateWithInt:(int)2];
[sha1 updateWithLong:(long)3];
[sha1 updateWithLongLong:(long long)4];
[sha1 updateWithFloat:(float)5];
[sha1 updateWithDouble:(double)6];

// Do final. 'final' method returns the pointer of the internal buffer
// that holds the message digest value. 'buffer' method returns the same.
// The life of the internal buffer ends when the SHA1 instance is discarded.
// Copy the buffer as necessary. The size of the buffer can be obtained by
// 'bufferSize' method.
unsigned char *digestAsBytes = [sha1 final];

// Get the string expression of the message digest value.
NSString *digestAsString = [sha1 description];

The message digest library supports MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512.

消息摘要库支持 MD5、SHA-1、SHA-224、SHA-256、SHA-384 和 SHA-512。

[Blog]Message digests (MD5, SHA1, etc.) on iOS with dedicated classes
http://darutk-oboegaki.blogspot.jp/2013/04/message-digests-md5-sha1-etc-on-ios.html

[博客]带有专用类的 iOS 上的消息摘要(MD5、SHA1 等)
http://darutk-oboegaki.blogspot.jp/2013/04/message-digests-md5-sha1-etc-on-ios.html

[Library]nv-ios-digest
https://github.com/TakahikoKawasaki/nv-ios-digest

[库]nv-ios-digest
https://github.com/TakahikoKawasaki/nv-ios-digest

回答by Billy ONeal

SHA1 doesn't actually come with Objective-C. You can use the C source code for hashdeepand friends, which is licensed under the public domain (Because it was written by an employee of the United States government): http://md5deep.sourceforge.net/.

SHA1 实际上并不与 Objective-C 一起提供。您可以将 C 源代码用于hashdeep和朋友,它是在公共领域许可的(因为它是由美国政府的一名雇员编写的):http: //md5deep.sourceforge.net/