简体   繁体   中英

Difference between bash-script and cocoa code with OpenSSL

I try get SHA1-sugnature with bash-script and cocoa-code.

Bash-script:

echo -n "RF001" | openssl dgst -sha1

gives me:

3eb0c58821e30a235a402308acff357e6f3d9f41

Cocoa:

const unsigned char buffer[] = "RF001";
size_t buffer_size = sizeof(buffer);

uint8_t md[SHA_DIGEST_LENGTH];
SHA1(buffer, buffer_size, md);

NSData* data = [NSData dataWithBytes:md length:SHA_DIGEST_LENGTH];
NSLog([data description], nil);

writes to console:

<3ab9d9cc ece56e3f 48d00c3f f2a9216a 5b74bbce>

Why are not these two results same?

It was error in Cocoa-snippet. There is good code:

const char* buffer = "RF001";

uint8_t md[SHA_DIGEST_LENGTH];
SHA1((uint8_t*)buffer, strlen(buffer), md);

NSData* data = [NSData dataWithBytes:md length:SHA_DIGEST_LENGTH];
NSLog([data description], nil);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM