2010-08-12 3 views
29

Objective c'de bir dize veya sayı kümesini nasıl yansıtırım?Amaç C: SHA1

+0

nv-ios-sindirmek adanmış sınıfları ile iOS'ta (MD5, SHA1, vs.) sindirir dikkat çekici şekilde http://stackoverflow.com/questions/756492/objective-c-sample-code-for-hmac-sha1. Orada henüz cevap yok, ama ben bütünlük uğruna yazıyorum. – Eli

+0

Belirli bir platform için bu mu? – ThomasW

+0

@ThomasW Mac os x sadece – Daniel

cevap

61

CommonCrypto (Apple framework) tek adımlı karma, SHA-1 karmaları hesaplanması dahil olmak üzere işlevi vardır: sayı dizisine için

#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'. */ 
    ... 
} 

, bize bilinen bir ints dizisi anlamına varsayalım uzunluğu. Bu hesaba endianness'ın almaz

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'. */ 
... 

Not: Bu tür veriler için, tek seferlik işlevini kullanmak daha iteratif yerine özetini oluşturmak daha kolaydır. Bir PowerPC sisteminde bu kod ile hesaplanan SHA-1, bir i386 veya ARM sisteminde hesaplanandan farklı olacaktır. Çözelti basit - hesaplama yapmadan önce değiştirilebilir bilinen bir endian tamsayılar bayt (NV-IOS sindirimi)

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

Ortak Kripto artık SDK'nın dışında değil – Daniel

+1

4.0.2'den itibaren evet öyle. Git ve dene! –

+1

Elbette, bu iOS'a özgü bir şey. Objective-C dili ile gelmiyor. –

2

SHA1 aslında Objective-C ile gelmiyor. hashdeep için C kaynak kodunu ve kamu malı altında lisanslı olan arkadaşlarınızı kullanabilirsiniz (ABD hükümeti çalışanı tarafından yazıldığı için): http://md5deep.sourceforge.net/.

+0

GnuPG (http://www.gnupg.org/related_software/libraries.en.html#lib-libgcrypt) yapımcılarından başka bir seçenek libgcrypt olacaktır. – schot

+0

Doğrudan c ve php için desteklenen herhangi bir güvenli şifreleme var mı? – Daniel

+1

@schot: Ancak libcrypt'in çok daha kısıtlayıcı bir lisansı var. @Daniel: Kutunun dışında değil, hayır. Objective-C kütüphane yolunda pek fazla gelmez. SHA1 içeren –

4

bir mesaj özeti kütüphane ile bir başka çözüm:

(1) Yaylı

// 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) Numaraları

// 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]; 

İleti özet kitaplığı MD5, SHA-1, SHA-224, SHA-256, SHA-384 ve SHA-512'yi destekler.

[blog] İleti
http://darutk-oboegaki.blogspot.jp/2013/04/message-digests-md5-sha1-etc-on-ios.html

[Kütüphane] Bu soru
https://github.com/TakahikoKawasaki/nv-ios-digest