SHA256 Swift to Objective C equivalence











up vote
-1
down vote

favorite












Hello everyone I'm working for the first time with SHA256 and I'm trying to follow a tutorial on this my problem is to write the equivalence in Objective C of SHA 256. I'm trying to understand the function that I show you below but I still have problems on how to find the equivalence in Objective C of this Swift function



let rsa2048Asn1Header:[UInt8] = [
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00
]

private func sha256(data : Data) -> String {
var keyWithHeader = Data(bytes: rsa2048Asn1Header)
keyWithHeader.append(data)
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
keyWithHeader.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(keyWithHeader.count), &hash)
}
return Data(hash).base64EncodedString()
}


Can you help me ?










share|improve this question






















  • Did you search?
    – vadian
    Nov 18 at 14:04










  • I tried to see many examples but I did not want to go wrong with the equivalent in Objective C of that function ... They look very different from each other
    – kAiN
    Nov 18 at 14:07










  • Try it out! If you get the sane result in Swift and ObjC you found the right solution. Actually Stackoverflow is not a code writing / code conversion service.
    – vadian
    Nov 18 at 14:10

















up vote
-1
down vote

favorite












Hello everyone I'm working for the first time with SHA256 and I'm trying to follow a tutorial on this my problem is to write the equivalence in Objective C of SHA 256. I'm trying to understand the function that I show you below but I still have problems on how to find the equivalence in Objective C of this Swift function



let rsa2048Asn1Header:[UInt8] = [
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00
]

private func sha256(data : Data) -> String {
var keyWithHeader = Data(bytes: rsa2048Asn1Header)
keyWithHeader.append(data)
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
keyWithHeader.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(keyWithHeader.count), &hash)
}
return Data(hash).base64EncodedString()
}


Can you help me ?










share|improve this question






















  • Did you search?
    – vadian
    Nov 18 at 14:04










  • I tried to see many examples but I did not want to go wrong with the equivalent in Objective C of that function ... They look very different from each other
    – kAiN
    Nov 18 at 14:07










  • Try it out! If you get the sane result in Swift and ObjC you found the right solution. Actually Stackoverflow is not a code writing / code conversion service.
    – vadian
    Nov 18 at 14:10















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Hello everyone I'm working for the first time with SHA256 and I'm trying to follow a tutorial on this my problem is to write the equivalence in Objective C of SHA 256. I'm trying to understand the function that I show you below but I still have problems on how to find the equivalence in Objective C of this Swift function



let rsa2048Asn1Header:[UInt8] = [
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00
]

private func sha256(data : Data) -> String {
var keyWithHeader = Data(bytes: rsa2048Asn1Header)
keyWithHeader.append(data)
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
keyWithHeader.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(keyWithHeader.count), &hash)
}
return Data(hash).base64EncodedString()
}


Can you help me ?










share|improve this question













Hello everyone I'm working for the first time with SHA256 and I'm trying to follow a tutorial on this my problem is to write the equivalence in Objective C of SHA 256. I'm trying to understand the function that I show you below but I still have problems on how to find the equivalence in Objective C of this Swift function



let rsa2048Asn1Header:[UInt8] = [
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00
]

private func sha256(data : Data) -> String {
var keyWithHeader = Data(bytes: rsa2048Asn1Header)
keyWithHeader.append(data)
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
keyWithHeader.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(keyWithHeader.count), &hash)
}
return Data(hash).base64EncodedString()
}


Can you help me ?







ios objective-c swift nsdata sha256






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 13:57









kAiN

8901438




8901438












  • Did you search?
    – vadian
    Nov 18 at 14:04










  • I tried to see many examples but I did not want to go wrong with the equivalent in Objective C of that function ... They look very different from each other
    – kAiN
    Nov 18 at 14:07










  • Try it out! If you get the sane result in Swift and ObjC you found the right solution. Actually Stackoverflow is not a code writing / code conversion service.
    – vadian
    Nov 18 at 14:10




















  • Did you search?
    – vadian
    Nov 18 at 14:04










  • I tried to see many examples but I did not want to go wrong with the equivalent in Objective C of that function ... They look very different from each other
    – kAiN
    Nov 18 at 14:07










  • Try it out! If you get the sane result in Swift and ObjC you found the right solution. Actually Stackoverflow is not a code writing / code conversion service.
    – vadian
    Nov 18 at 14:10


















Did you search?
– vadian
Nov 18 at 14:04




Did you search?
– vadian
Nov 18 at 14:04












I tried to see many examples but I did not want to go wrong with the equivalent in Objective C of that function ... They look very different from each other
– kAiN
Nov 18 at 14:07




I tried to see many examples but I did not want to go wrong with the equivalent in Objective C of that function ... They look very different from each other
– kAiN
Nov 18 at 14:07












Try it out! If you get the sane result in Swift and ObjC you found the right solution. Actually Stackoverflow is not a code writing / code conversion service.
– vadian
Nov 18 at 14:10






Try it out! If you get the sane result in Swift and ObjC you found the right solution. Actually Stackoverflow is not a code writing / code conversion service.
– vadian
Nov 18 at 14:10














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Working with raw bytes in Objective-C is generally a little more straightforward than Swift. An implementation like this should be equivalent.



#define RSA_2048_ASN1_HDR_LEN 24

- (NSString *)sha256:(NSData *)data {
NSMutableData *keyWithHeader = [NSMutableData dataWithBytes:rsa2048Asn1Header length:RSA_2048_ASN1_HDR_LEN];
[keyWithHeader appendData:data];
UInt8 hash[CC_SHA256_DIGEST_LENGTH] = { 0 };
CC_SHA256(keyWithHeader.bytes, (CC_LONG) keyWithHeader.length, hash);
return [[NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH] base64EncodedStringWithOptions:0];
}


Note that you'll also need to import the common crypto library into your Objective-C file as well:



#import <CommonCrypto/CommonDigest.h>





share|improve this answer





















  • You were very kind ... thank you so much!
    – kAiN
    Nov 18 at 15:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361681%2fsha256-swift-to-objective-c-equivalence%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Working with raw bytes in Objective-C is generally a little more straightforward than Swift. An implementation like this should be equivalent.



#define RSA_2048_ASN1_HDR_LEN 24

- (NSString *)sha256:(NSData *)data {
NSMutableData *keyWithHeader = [NSMutableData dataWithBytes:rsa2048Asn1Header length:RSA_2048_ASN1_HDR_LEN];
[keyWithHeader appendData:data];
UInt8 hash[CC_SHA256_DIGEST_LENGTH] = { 0 };
CC_SHA256(keyWithHeader.bytes, (CC_LONG) keyWithHeader.length, hash);
return [[NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH] base64EncodedStringWithOptions:0];
}


Note that you'll also need to import the common crypto library into your Objective-C file as well:



#import <CommonCrypto/CommonDigest.h>





share|improve this answer





















  • You were very kind ... thank you so much!
    – kAiN
    Nov 18 at 15:11















up vote
2
down vote



accepted










Working with raw bytes in Objective-C is generally a little more straightforward than Swift. An implementation like this should be equivalent.



#define RSA_2048_ASN1_HDR_LEN 24

- (NSString *)sha256:(NSData *)data {
NSMutableData *keyWithHeader = [NSMutableData dataWithBytes:rsa2048Asn1Header length:RSA_2048_ASN1_HDR_LEN];
[keyWithHeader appendData:data];
UInt8 hash[CC_SHA256_DIGEST_LENGTH] = { 0 };
CC_SHA256(keyWithHeader.bytes, (CC_LONG) keyWithHeader.length, hash);
return [[NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH] base64EncodedStringWithOptions:0];
}


Note that you'll also need to import the common crypto library into your Objective-C file as well:



#import <CommonCrypto/CommonDigest.h>





share|improve this answer





















  • You were very kind ... thank you so much!
    – kAiN
    Nov 18 at 15:11













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Working with raw bytes in Objective-C is generally a little more straightforward than Swift. An implementation like this should be equivalent.



#define RSA_2048_ASN1_HDR_LEN 24

- (NSString *)sha256:(NSData *)data {
NSMutableData *keyWithHeader = [NSMutableData dataWithBytes:rsa2048Asn1Header length:RSA_2048_ASN1_HDR_LEN];
[keyWithHeader appendData:data];
UInt8 hash[CC_SHA256_DIGEST_LENGTH] = { 0 };
CC_SHA256(keyWithHeader.bytes, (CC_LONG) keyWithHeader.length, hash);
return [[NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH] base64EncodedStringWithOptions:0];
}


Note that you'll also need to import the common crypto library into your Objective-C file as well:



#import <CommonCrypto/CommonDigest.h>





share|improve this answer












Working with raw bytes in Objective-C is generally a little more straightforward than Swift. An implementation like this should be equivalent.



#define RSA_2048_ASN1_HDR_LEN 24

- (NSString *)sha256:(NSData *)data {
NSMutableData *keyWithHeader = [NSMutableData dataWithBytes:rsa2048Asn1Header length:RSA_2048_ASN1_HDR_LEN];
[keyWithHeader appendData:data];
UInt8 hash[CC_SHA256_DIGEST_LENGTH] = { 0 };
CC_SHA256(keyWithHeader.bytes, (CC_LONG) keyWithHeader.length, hash);
return [[NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH] base64EncodedStringWithOptions:0];
}


Note that you'll also need to import the common crypto library into your Objective-C file as well:



#import <CommonCrypto/CommonDigest.h>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 at 14:31









ryanecrist

20328




20328












  • You were very kind ... thank you so much!
    – kAiN
    Nov 18 at 15:11


















  • You were very kind ... thank you so much!
    – kAiN
    Nov 18 at 15:11
















You were very kind ... thank you so much!
– kAiN
Nov 18 at 15:11




You were very kind ... thank you so much!
– kAiN
Nov 18 at 15:11


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361681%2fsha256-swift-to-objective-c-equivalence%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno