App-Authentication: Description
App-Authentication: Description
Description
For api_secret not be revealed, signature is used when calling App-GetBizToken and App-nuVerify to ensure the communication security.
Signature Algorithm
String: a=[api_key]&b=[expire_time]&c=[current_time]&d=[random]
Parameters
b expire_time The validity of signature, which is a number displayed in UNIX Epoch timestamp. Unit: second.
c current_time The timestamp when the signature is generated, in seconds. As a non-single signature, current_time
should be not older than expire_time.
Note: A single signature means that the generated sign is used only once; a non-single signature means that the sign is allowed to be used
multiple times for a period of time, and the time limit needs to be set.
Signature
Using HMAC-SHA1 algorithm to encrypt requests.
Formula:
Note:
The standard Base64 encoding is used here, not the Base64 encoding of urlsafe.
Api_secret must be used with api_key and can be obtained from FaceID console.
Sample Code
Python Code Sample
import time
import hashlib
import base64
import random
import hmac
api_key = "Your api_key"
api_secret = "Your api_secret"
valid_durtion = 100 # valid time is 100 seconds.
current_time = int(time.time())
expire_time = current_time + valid_durtion
rdm = ''.join(random.choice("0123456789") for i in range(10))
raw = "a={}&b={}&c={}&d={}".format(api_key, expire_time, current_time, rdm)
sign_tmp = hmac.new(api_secret, raw, hashlib.sha1).digest()
sign = base64.b64encode(sign_tmp + raw)
Java Code Sample
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Random;
/** * Generate signature * * @param apiKey * @param secretKey * @param expired * @return * @throws Exception
*/
public static String genSign(String apiKey, String secretKey, long expired) throws Exception {
long now = System.currentTimeMillis() / 1000;
int rdm = Math.abs(new Random().nextInt());
String plainText = String.format("a=%s&b=%d&c=%d&d=%d", apiKey, now + expired, now, rdm);
byte[] hmacDigest = HmacSha1(plainText, secretKey);
byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length,
plainText.getBytes().length);
return encodeToBase64(signContent);
}
/** * Generate hmacsha1 signature * * @param binaryData * @param key * @return * @throws Exception */
public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
mac.init(secretKey);
byte[] HmacSha1Digest = mac.doFinal(binaryData);
return HmacSha1Digest;
}
/** * Generate hmacsha1 signature * * @param plainText * @param key * @return * @throws Exception */
public static byte[] HmacSha1(String plainText, String key) throws Exception {
return HmacSha1(plainText.getBytes(), key);
}
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString* sign = [self getSignStr];
NSLog(@"sign = %@",sign);
}
- (NSString*)getSignStr {
int validdurtion = 10000;
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0]; //get the current time
NSTimeInterval currenttime = [date timeIntervalSince1970];
long int expiretime = currenttime + validdurtion;
long random = labs(arc4random() % 100000000000);
NSString* str = [NSString stringWithFormat:@"a=%@&b=%ld&c=%f&d=%ld" ,api_key,expiretime,currenttime,random];
const char *cKey = [api_secret cStringUsingEncoding:NSUTF8StringEn coding];
const char *cData = [str cStringUsingEncoding:NSUTF8StringEncoding];
char cHMAC[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cH MAC);
NSData *HMAC = [[NSData alloc]initWithBytes:cHMAC length:sizeof(cHM AC)];
NSData* sign_raw_data = [str dataUsingEncoding:NSUTF8StringEncoding ];
NSMutableData* data = [[NSMutableData alloc] initWithData:HMAC];
[data appendData:sign_raw_data];
NSString* sign = [data base64EncodedStringWithOptions:0];
return sign;
}
@end