설정

JSONStore 보안 유틸리티 API를 사용하려면 다음 파일을 가져오십시오.

iOS

#import "WLSecurityUtils.h"

Android

importcom.worklight.wlclient.api.SecurityUtils

JavaScript

설정이 필요하지 않습니다.

예제

iOS

암호화 및 복호화

// User provided password, hardcoded only for simplicity.NSString*password=@"HelloPassword";// Random salt with recommended length.NSString*salt=[WLSecurityUtilsgenerateRandomStringWithBytes:32];// Recomended number of iterations.intiterations=10000;// Populated with an error if one occurs.NSError*error=nil;// Call that generates the key.NSString*key=[WLSecurityUtilsgenerateKeyWithPassword:passwordandSalt:saltandIterations:iterationserror:&error];// Text that is encrypted.NSString*originalString=@"My secret text";NSDictionary*dict=[WLSecurityUtilsencryptText:originalStringwithKey:keyerror:&error];// Should return: 'My secret text'.NSString*decryptedString=[WLSecurityUtilsdecryptWithKey:keyandDictionary:dicterror:&error];

원격 랜덤 가져오기

[WLSecurityUtilsgetRandomStringFromServerWithBytes:32timeout:1000completionHandler:^(NSURLResponse*response,NSData*data,NSError*connectionError){// You might want to see the response and the connection error before moving forward.// Get the secure random string.NSString*secureRandom=[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];}];

Android

암호화 및 복호화

Stringpassword="HelloPassword";Stringsalt=SecurityUtils.getRandomString(32);intiterations=10000;Stringkey=SecurityUtils.generateKey(password,salt,iterations);StringoriginalText="Hello World!";JSONObjectencryptedObject=SecurityUtils.encrypt(key,originalText);// Deciphered text will be the same as the original text.StringdecipheredText=SecurityUtils.decrypt(key,encryptedObject);

base64로 인코딩 및 디코딩

importandroid.util.Base64;StringoriginalText="Hello World";byte[]base64Encoded=Base64.encode(text.getBytes("UTF-8"),Base64.DEFAULT);StringencodedText=newString(base64Encoded,"UTF-8");byte[]base64Decoded=Base64.decode(text.getBytes("UTF-8"),Base64.DEFAULT);// Decoded text will be the same as the original text.StringdecodedText=newString(base64Decoded,"UTF-8");

원격 랜덤 가져오기

Contextcontext;// This is the current Activity's context.intbyteLength=32;// Listener calls the callback functions after it gets the response from the server.WLRequestListenerlistener=newWLRequestListener(){@OverridepublicvoidonSuccess(WLResponsewlResponse){// Implement the success handler.}@OverridepublicvoidonFailure(WLFailResponsewlFailResponse){// Implement the failure handler.}};SecurityUtils.getRandomStringFromServer(byteLength,context,listener);

로컬 랜덤 가져오기

JavaScript

암호화 및 복호화

// Keep the key in a variable so that it can be passed to the encrypt and decrypt API.varkey;// Generate a key.WL.SecurityUtils.keygen({password:'HelloPassword',salt:Math.random().toString(),iterations:10000}).then(function(res){// Update the key variable.key=res;// Encrypt text.returnWL.SecurityUtils.encrypt({key:key,text:'My secret text'});}).then(function(res){// Append the key to the result object from encrypt.res.key=key;// Decrypt.returnWL.SecurityUtils.decrypt(res);}).then(function(res){// Remove the key from memory.key=null;//res => 'My secret text'}).fail(function(err){// Handle failure in any of the previously called APIs.});