首页 > 解决方案 > 如何将 Google PRIVATE_KEY 转换为 Base64 编码签名

问题描述

我如何将 google 转换Private_keyBase64 Encoded Signature. 我在objective-c中做了同样的实现,但是我在swift中找不到同样的实现。拜托,任何人都可以帮助我...在此先感谢

NSString *key = keys.gOOGLE_PRIVATE_KEY;
NSString *url = [NSString stringWithFormat:@"/maps/api/directions/json?%@&sensor=false&client=%@", query, keys.gOOGLE_CLIENT_ID];

// Stores the url in a NSData.
NSData *urlData = [url dataUsingEncoding:NSUTF8StringEncoding];

// URL-safe Base64 coder/decoder.
GTMStringEncoding *encoding = [GTMStringEncoding rfc4648Base64WebsafeStringEncoding];

// Decodes the URL-safe Base64 key to binary.
NSData *binaryKey = [encoding decode:key];

// Signs the URL.
unsigned char result[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1,
               [binaryKey bytes], [binaryKey length],
               [urlData bytes], [urlData length],
               &result);
NSData *binarySignature = [NSData dataWithBytes:&result length:CC_SHA1_DIGEST_LENGTH];

// Encodes the signature to URL-safe Base64.
NSString *signature = [encoding encode:binarySignature];

标签: iosswiftxcodeswift4.2swift5

解决方案


推荐阅读