Skip to content

Instantly share code, notes, and snippets.

View lkdocs's full-sized avatar

LaunchKey Documentation Examples lkdocs

View GitHub Profile
private byte[] signWithPrivateKey(byte[] bytes, String privateKey) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSA", "SC");
signature.initSign(Crypto.getRSAPrivateKeyFromString(privateKey));
signature.update(bytes);
return signature.sign();
}
public static KeyPair generateRSAKeyPair() throws Exception {
SecureRandom random = new SecureRandom();
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(KEY_SIZE, RSAKeyGenParameterSpec.F4);
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "SC");
generator.initialize(spec, random);
return generator.generateKeyPair();
}
public static PublicKey getRSAPublicKeyFromString(String apiKey) throws Exception{
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC");
byte[] publicKeyBytes = Base64.decode(apiKey.getBytes("UTF-8"), Base64.DEFAULT);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
return keyFactory.generatePublic(x509KeySpec);
}
public static PrivateKey getRSAPrivateKeyFromString(String key) throws Exception {
byte [] clear = Base64.decode(key, Base64.DEFAULT);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
+(NSString *)encryptRSA:(NSString *)plainTextString key:(SecKeyRef)publicKey {
size_t cipherBufferSize = SecKeyGetBlockSize(publicKey);
uint8_t *cipherBuffer = malloc(cipherBufferSize);
uint8_t *nonce = (uint8_t *)[plainTextString UTF8String];
SecKeyEncrypt(publicKey,
kSecPaddingOAEP,
nonce,
strlen( (char*)nonce ),
&cipherBuffer[0],
&cipherBufferSize);
+(NSString *)decryptRSA:(NSString *)cipherString key:(SecKeyRef) privateKey {
size_t plainBufferSize = SecKeyGetBlockSize(privateKey);
uint8_t *plainBuffer = malloc(plainBufferSize); NSData *incomingData = [NSData dataFromBase64String:cipherString];
uint8_t *cipherBuffer = (uint8_t*)[incomingData bytes];
size_t cipherBufferSize = SecKeyGetBlockSize(privateKey);
SecKeyDecrypt(privateKey,
kSecPaddingOAEP,
cipherBuffer,
cipherBufferSize,
plainBuffer,
+ (NSData *)getSignatureBytes:(NSData *)plainText withPrivateKey:(SecKeyRef)privateKey
{
OSStatus sanityCheck = noErr;
NSData * signedHash = nil;
uint8_t *signedHashBytesSize = SecKeyGetBlockSize(privateKey);
size_t signedHashBytes = malloc( signedHashBytesSize * sizeof(uint8_t) );
memset((void *)signedHashBytes, 0x0, signedHashBytesSize);
sanityCheck = SecKeyRawSign(privateKey,
+(void)generateKeyPairWithPublicTag:(NSString *)publicTagString privateTag:(NSString *)privateTagString {
NSMutableDictionary *privateKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary *publicKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary *keyPairAttr = [[NSMutableDictionary alloc] init];
NSData *publicTag = [publicTagString dataUsingEncoding:NSUTF8StringEncoding];
NSData *privateTag = [privateTagString dataUsingEncoding:NSUTF8StringEncoding];
SecKeyRef publicKey = NULL;
SecKeyRef privateKey = NULL;
curl https://api.launchkey.com/v1/ping
curl -X POST https://api.launchkey.com/v1/auths \
-d app_key=1234567890 \
--data-urlencode secret_key=SECRET_KEY \
--data-urlencode signature=SIGNATURE \
-d username=USERNAME \
-d session=1
curl https://api.launchkey.com/v1/poll \
-d app_key=1234567890 \
--data-urlencode secret_key=SECRET_KEY \
--data-urlencode signature=SIGNATURE \
-d auth_request=AUTH_REQUEST