import {PairRequest, KeyResponse, SecretsAndKeyResponse, KeyRequest} from './Pairing';
import {Secrets} from './Secrets';
import {Crypto} from './Crypto';
import {DiffieHellman} from './DiffieHellman';
// This is the generator used for diffie-hellman in 2048-bit MODP Group 14 as per (https://tools.ietf.org/html/rfc3526#section-3)
export const GENERATOR = 2;
// This is the prime used for diffie-hellman using 2048-bit MODP Group 14 as per (https://tools.ietf.org/html/rfc3526#section-3)
export const GROUP14_2048_BIT_MODP = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF';
//
// This static class helps you with the pairing process as documented here:
// http://www.simplepaymentapi.com/#/api/pairing-process
//
export class PairingHelper {
//
// Generates a pairing Request.
//
// New PairRequest
static NewPairRequest() {
return new PairRequest();
}
//
// Calculates/Generates Secrets and KeyResponse given an incoming KeyRequest.
//
//
// Secrets and KeyResponse to send back.
GenerateSecretsAndKeyResponse(keyRequest: KeyRequest) {
const encPubAndSec = this._calculateMyPublicKeyAndSecret(keyRequest.Aenc);
const Benc = encPubAndSec.MyPublicKey;
const Senc = encPubAndSec.SharedSecretKey;
const hmacPubAndSec = this._calculateMyPublicKeyAndSecret(keyRequest.Ahmac);
const Bhmac = hmacPubAndSec.MyPublicKey;
const Shmac = hmacPubAndSec.SharedSecretKey;
const secrets = new Secrets(Senc, Shmac);
const keyResponse = new KeyResponse(keyRequest.RequestId, Benc, Bhmac);
return new SecretsAndKeyResponse(secrets, keyResponse);
}
//
// Turns an incoming "A" value from the PinPad into the outgoing "B" value
// and the secret value using DiffieHelmman helper.
//
// The incoming A value
// Your B value and the Secret
_calculateMyPublicKeyAndSecret(theirPublicKey: string) {
const diffieHellman = new DiffieHellman();
const myPrivateBI = diffieHellman.RandomPrivateKey(GROUP14_2048_BIT_MODP);
const myPublicBI = diffieHellman.PublicKey(GROUP14_2048_BIT_MODP, GENERATOR, myPrivateBI);
const secretBI = diffieHellman.Secret(GROUP14_2048_BIT_MODP, theirPublicKey, myPrivateBI);
const secret = this.DHSecretToSPISecret(secretBI);
return new PublicKeyAndSecret(myPublicBI, secret);
}
//
// Converts the DH secret BigInteger into the hex-string to be used as the secret.
// There are some "gotchyas" here which is why this piece of work is abstracted so it can be tested separately.
// See: http://www.simplepaymentapi.com/#/api/pairing-process
//
// Secret as BigInteger
// Secret as Hex-String
DHSecretToSPISecret(secret: string)
{
// If the calculated hexadecimal secret doesn't have an even number of characters, we add an extra 0 to the start. This allows SHA-256 to operate on the hexadecimal secret as if it were a hexadecimal representation of a string.
if (secret.length % 2 === 1){
secret = '0' + secret;
}
secret = secret.padStart(512, '0');
// We sha256 that byte array and return the hex string result
return Crypto.GenerateHash(secret);
}
}
//
// Internal Holder class for Public and Secret, so that we can use them together in method signatures.
//
export class PublicKeyAndSecret {
public MyPublicKey: string;
public SharedSecretKey: string;
constructor(myPublicKey: string, sharedSecretKey: string) {
this.MyPublicKey = myPublicKey;
this.SharedSecretKey = sharedSecretKey;
}
}