use of com.fitpay.android.api.models.security.ECCKeyPair in project fitpay-android-sdk by fitpay.
the class StringUtils method getDecryptedString.
/**
* Get decrypted string
*
* @param type key type
* @param encryptedString encrypted string
* @return decrypted string
*/
public static String getDecryptedString(@KeysManager.KeyType int type, String encryptedString) {
KeysManager keysManager = KeysManager.getInstance();
JWEObject jweObject;
try {
jweObject = JWEObject.parse(encryptedString);
JWEHeader jweHeader = jweObject.getHeader();
if (jweHeader.getKeyID() == null || jweHeader.getKeyID().equals(keysManager.getKeyId(type))) {
jweObject.decrypt(new AESDecrypter(keysManager.getSecretKey(type)));
if ("JWT".equals(jweObject.getHeader().getContentType())) {
SignedJWT signedJwt = jweObject.getPayload().toSignedJWT();
ECCKeyPair keyPair = keysManager.getPairForType(type);
ECPublicKey key = null;
if ("https://fit-pay.com".equals(signedJwt.getJWTClaimsSet().getIssuer())) {
key = (ECPublicKey) keysManager.getPublicKey("EC", Hex.hexStringToBytes(keyPair.getServerPublicKey()));
} else {
key = (ECPublicKey) keysManager.getPublicKey("EC", Hex.hexStringToBytes(keyPair.getPublicKey()));
}
JWSVerifier verifier = new ECDSAVerifier(key);
if (!signedJwt.verify(verifier)) {
throw new IllegalArgumentException("jwt did not pass signature validation");
}
return signedJwt.getJWTClaimsSet().getStringClaim("data");
} else {
return jweObject.getPayload().toString();
}
}
} catch (Exception e) {
FPLog.e(e);
}
return null;
}
use of com.fitpay.android.api.models.security.ECCKeyPair in project fitpay-android-sdk by fitpay.
the class StringUtils method getEncryptedString.
/**
* Get encrypted string
*
* @param type key type
* @param decryptedString decrypted string
* @return encrypted string
*/
public static String getEncryptedString(@KeysManager.KeyType int type, String decryptedString) {
JWEAlgorithm alg = JWEAlgorithm.A256GCMKW;
EncryptionMethod enc = EncryptionMethod.A256GCM;
ECCKeyPair keyPair = KeysManager.getInstance().getPairForType(type);
if (null == keyPair) {
throw new IllegalStateException("No key pair available for type (type = " + type + ")");
}
JWEHeader.Builder jweHeaderBuilder = new JWEHeader.Builder(alg, enc).contentType("application/json").keyID(keyPair.getKeyId());
JWEHeader header = jweHeaderBuilder.build();
Payload payload = new Payload(decryptedString);
JWEObject jweObject = new JWEObject(header, payload);
try {
JWEEncrypter encrypter = new AESEncrypter(KeysManager.getInstance().getSecretKey(type));
jweObject.encrypt(encrypter);
} catch (JOSEException e) {
FPLog.e(e);
}
return jweObject.serialize();
}
use of com.fitpay.android.api.models.security.ECCKeyPair in project fitpay-android-sdk by fitpay.
the class KeysManager method createPairForType.
public ECCKeyPair createPairForType(@KeyType int type) throws Exception {
removePairForType(type);
ECCKeyPair keyPair = createECCKeyPair();
mKeysMap.put(type, keyPair);
return keyPair;
}
use of com.fitpay.android.api.models.security.ECCKeyPair in project fitpay-android-sdk by fitpay.
the class KeysManager method createECCKeyPair.
// Create the public and private keys
private ECCKeyPair createECCKeyPair() throws Exception {
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(ALGORITHM, SecurityProvider.getInstance().getProvider());
keyGenerator.initialize(new ECGenParameterSpec(EC_CURVE), new SecureRandom());
KeyPair keyPair = keyGenerator.generateKeyPair();
ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
ECCKeyPair eccKeyPair = new ECCKeyPair();
eccKeyPair.setKeyId(UUID.randomUUID().toString());
eccKeyPair.setPrivateKey(Hex.bytesToHexString(privateKey.getEncoded()));
eccKeyPair.setPublicKey(Hex.bytesToHexString(publicKey.getEncoded()));
return eccKeyPair;
}
use of com.fitpay.android.api.models.security.ECCKeyPair in project fitpay-android-sdk by fitpay.
the class KeysManager method getSecretKey.
public SecretKey getSecretKey(@KeyType int type) {
ECCKeyPair keyPair = getPairForType(type);
SecretKey secretKey = keyPair.getSecretKey();
if (secretKey == null) {
secretKey = createSecretKey(keyPair.getPrivateKey(), keyPair.getServerPublicKey());
keyPair.setSecretKey(secretKey);
}
return secretKey;
}
Aggregations