简体   繁体   English

将NSData转换为NSString后如何将NSString转换回NSData?

[英]How to convert NSString back to NSData after converting NSData to NSString?

I am generating SHA-256 key of a file using the function given below: 我正在使用下面给出的功能生成文件的SHA-256密钥:

- (NSData *)doSha256:(NSData *)dataIn {
    NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];

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

    return macOut;
}

This function is generating SHA-256 key and returning NSData however, I need to store the key in the database in String format. 此函数正在生成SHA-256密钥并返回NSData,但是,我需要以String格式将密钥存储在数据库中。 In order to convert the NSData to NSString I am using the code given below: 为了将NSData转换为NSString,我使用下面给出的代码:

//converting sha256 to nsstring
    NSString * str = [sha256 base64EncodedStringWithOptions:0];

Then 然后

I am trying to convert the str back to NSData using this code: 我正在尝试使用以下代码将str转换回NSData:

//converting str back to nsdata
    NSData* dataFrmString = [str dataUsingEncoding:NSUTF8StringEncoding];

Problem 问题

When I am trying to compare the dataFrmString and sha256 its saying the both NSData are not same 当我尝试比较dataFrmStringsha256时,说两个NSData不相同

//matching if the dataFrmString is equal to the sha256 data

if([dataFrmString isEqualTo:sha256])
{
    NSLog(@"sucessully back to nsdata");
}

Here is the whole code 这是整个代码

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application

    // source file : whose sha 256 will be generated
    NSString* sourceFile = @"/Users/Paxcel/Downloads/Movies/World4uFRee.cc_dsam7dds.mkv";

    //getting nsdata of the file
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:sourceFile];

    //getting sha 256 of the file
    NSData *sha256 = [self doSha256:data];

    //converting sha256 to nsstring
    NSString * str = [sha256 base64EncodedStringWithOptions:0];

    //converting str back to nsdat
    NSData* dataFrmString = [[NSData alloc] initWithBase64EncodedString:str
                                                                options:0];
    //matching if the dataFrmString is equal to the sha256 data
    if([dataFrmString isEqualTo:sha256])
    {
        NSLog(@"sucessully back to nsdata");
    }



    NSString* destinationFile = @"/Users/Paxcel/Desktop/appcast2.xml";
    [sha256 writeToFile:destinationFile atomically:YES];


}

- (NSData *)doSha256:(NSData *)dataIn {
    NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];

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

    return macOut;
}

You need to decode the base-64 encoded string, with something like: 您需要使用以下代码对base-64编码的字符串进行解码:

NSData *data = [[NSData alloc] initWithBase64EncodedString:str
                                                   options:0];

Where str is the string read from the database. 其中str是从数据库读取的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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