在iOS SDK中创建密码摘要

在iOS SDK中创建密码摘要,第1张

概述我正在我的iPhone应用程序中集成DigestPassword Web服务.为此,我需要生成nonce和passwordDigest.我努力了,但在Google搜索中找不到任何有用的例子. 我按照以下方式完成了:更新的代码: NSString *user = @"XXXXXXXXXXXXXXXXXXXXXX";NSNumber *nonce = @(arc4random());NSLog(@ 我正在我的iPhone应用程序中集成DigestPassword Web服务.为此,我需要生成nonce和passwordDigest.我努力了,但在Google搜索中找不到任何有用的例子.

我按照以下方式完成了:更新的代码:

Nsstring *user = @"XXXXXXXXXXXXXXXXXXXXXX";NSNumber *nonce = @(arc4random());NSLog(@"nonce %@",[self encodeStringTo64:[nonce stringValue]]);NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];Nsstring *created = [dateFormatter stringFromDate:[NSDate date]];NSLog(@"created %@",created);Nsstring *digest_concat = [Nsstring stringWithFormat:@"%@%@%@",nonce,created,@"Pwd@123"];NSData *digestBytes = [self shaa1:digest_concat];Nsstring *digestBase64 = [self base64forData:digestBytes];NSLog(@"digestBase64 %@",digestBase64);Nsstring *strSOAP = [Nsstring stringWithFormat:@"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:v1=\"http://sita.aero/iborders/external/ReferralManagementServiceWSDLType/V1\"><soap:header><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><wsse:UsernameToken wsu:ID=\"UsernameToken-27E173B8CF239BE6F01440582357416191\"><wsse:Username>%@</wsse:Username><wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">%@</wsse:Password><wsse:Nonce EnCodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">%@</wsse:Nonce><wsu:Created>%@</wsu:Created></wsse:UsernameToken></wsse:Security></soap:header><soap:Body><v1:SearchreferralsRequest><v1:ReferralSearchCriteria><ReferralID>2038100</ReferralID></v1:ReferralSearchCriteria><v1:Paging><FetchNumber>1</FetchNumber><ResultsPerFetch>1</ResultsPerFetch></v1:Paging></v1:SearchreferralsRequest></soap:Body></soap:Envelope>",user,digestBase64,[self encodeStringTo64:[nonce stringValue]],created];- (Nsstring*)encodeStringTo64:(Nsstring*)fromString{    NSData *plainData = [fromString dataUsingEnCoding:NSUTF8StringEnCoding];    Nsstring *base64String;    if ([plainData respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {        base64String = [plainData base64EncodedStringWithOptions:kNilOptions];  // iOS 7+    } else {        base64String = [plainData base64EnCoding];                              // pre iOS7    }    return base64String;}- (NSData *)shaa1:(Nsstring *)input {NSData *data = [input dataUsingEnCoding:NSUTF8StringEnCoding];uint8_t digest[CC_SHA1_DIGEST_LENGTH];CC_SHA1(data.bytes,(CC_LONG) data.length,digest);return [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];

}

有人可以检查我的代码创建nonce和密码摘要,让我知道我正在做什么错误,因为我无法使用此访问Web服务?我总是收到Message Expired错误.
错误:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">   <soap:Body>      <soap:Fault>         <soap:Code>            <soap:Value>soap:Sender</soap:Value>            <soap:Subcode>               <soap:Value xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">ns1:MessageExpired</soap:Value>            </soap:Subcode>         </soap:Code>         <soap:Reason>            <soap:Text xml:lang="en">The message has expired</soap:Text>         </soap:Reason>      </soap:Fault>   </soap:Body></soap:Envelope>

编辑:我想要的SOAP请求:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v1="http://xxxxx/xxxx/external/ServiceWSDLType/V1"> <soap:header>    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">      <wsse:UsernameToken wsu:ID="UsernameToken-XXXXX">          <wsse:Username>XXXXXXXXXXXXXXXXXXX</wsse:Username>          <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">iAdbggkXsbNih5wBJ8M2tyyVWiA=</wsse:Password>          <wsse:Nonce EnCodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">b8SD2g2iiVvihx7ajVwPfw==</wsse:Nonce>          <wsu:Created>2015-08-28T07:16:04.857Z</wsu:Created>       </wsse:UsernameToken>    </wsse:Security> </soap:header> <soap:Body>    <v1:SearchreferralsRequest>       <v1:ReferralSearchCriteria>          <ReferralID>213213</ReferralID>       </v1:ReferralSearchCriteria>       <v1:Paging>          <FetchNumber>1</FetchNumber>          <ResultsPerFetch>1</ResultsPerFetch>       </v1:Paging>    </v1:SearchreferralsRequest> </soap:Body></soap:Envelope>
解决方法 我认为您需要将SHA1摘要无需额外编码发送到十六进制字符串.

试试SHA1的这个功能:

- (NSData *)sha1:(Nsstring *)input {    NSData *data = [input dataUsingEnCoding:NSUTF8StringEnCoding];    uint8_t digest[CC_SHA1_DIGEST_LENGTH];    CC_SHA1(data.bytes,digest);    return [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];}

还要检查密码摘要中的时间值是否与wsu:Created相同.

更新#1

您还在密码摘要中使用base64 nonce,但在文档公式中是:

Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) )

所以nonce必须是数字,而不是base64编码的数字.试试这个密码摘要代码:

NSNumber *nonce = @(arc4random());NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];Nsstring *created = [dateFormatter stringFromDate:[NSDate date]];Nsstring *digest_concat = [Nsstring stringWithFormat:@"%@%@%@",password];

更新#2

由于时区,您似乎已过期消息.从规范Web服务安全性:SOAP消息安全性1.1.1版:

This specification defines and illustrates time references in terms of
the xsd:dateTime type defined in XML Schema. It is RECOMMENDED that
all time references use this type. All references MUST be in UTC
time. Implementations MUST NOT generate time instants that specify
leap seconds. If,however,other time types are used,then the
ValueType attribute (described below) MUST be specifIEd to indicate
the data type of the time format. Requestors and receivers SHOulD NOT
rely on other applications supporting time resolution finer than
milliseconds.

使用UTC时区:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];Nsstring *created = [dateFormatter stringFromDate:[NSDate date]];
总结

以上是内存溢出为你收集整理的在iOS SDK中创建密码摘要全部内容,希望文章能够帮你解决在iOS SDK中创建密码摘要所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1027337.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存