Signature / Signing using a SHA256 Algorithm in Apex

PHOTO EMBED

Mon Jul 04 2022 15:54:08 GMT+0000 (UTC)

Saved by @Justus #apex

// Data and private key. Use a method for getting the keys and signature
// so the example is more readable
private final String DATA		= 'The quick brown fox jumps over the lazy dog';
private final String SIGNATURE	= getSignature();
private final String PUBLIC_KEY	= getPublicKey();
private final String PRIVATE_KEY= getPrivateKey();

// Performance measuring
Integer startTime = Limits.getCpuTime();

// Validate the newly created signature is as expected
System.assertEquals(
	SIGNATURE,
	createSignature(DATA, PRIVATE_KEY),
	'The expected signature does not match the actual signature created using the private key.'
);


// VERIFY the signature is as expected
System.assertEquals(
	true,
	verifySignature(
		DATA,
		SIGNATURE,
		PUBLIC_KEY
	),
	'Error verifying the signature using the public key'
);


// If no errors let us know, also output the time it took, as signatures might be CPU intensive
System.debug('All Key tests were OK. Execution took: ' +( Limits.getCpuTime() - startTime) + 'ms');


/**
 * Method to get the signature when signing some data
 * @param dataToSign	The data that needs to be signed
 * @param privateKey	The RSA Private key
 */
public String createSignature(String dataToSign, String privateKey){
	return EncodingUtil.base64Encode(
		Crypto.sign(
			'RSA-SHA256', // The algorith you want to use
			Blob.valueOf(dataToSign), // Convert the string to a blog
			EncodingUtil.base64Decode(privateKey) // decode converts string into a blob
		)
	);
}


/**
 * Method to verify a signature
 * @param dataToVerify	The data that needs to be verified
 * @param signature		The B64 encoded signature that needs to be verified
 * @param publicKey		The B64 encoded RSA Public key
 */
public Boolean verifySignature(String dataToVerify, String signature, String publicKey){
	return Crypto.verify(
		'RSA-SHA256', // The algorith you want to use
		Blob.valueOf(dataToVerify), // Convert to blob
		EncodingUtil.base64Decode(signature), // Creates Blob from String
		EncodingUtil.base64Decode(publicKey)  // Creates Blob from String
	);
}
	

/**
 * Method to get the expected signature. This is for testing only, just so you have an
 * idea what the output of the signature looks like
 */ 
private String getSignature(){
	return 'RzgwUN39Dicag301cyf79neCKTZjotoguttb34gZjKKvT3kRj1ZvBtmynqHIRWBET0vDKugtKg/MtPu5h1Y8hqSHmZnkXbld/i737ptVEB6PQdRp26izWlLqpEyzkGrJ/FuJYyRNEV1xJdrnsTP3nBZ8qB0NPwCr9ZInZOWfO3DKspCOBWGaQ7H2ewC1OEr0OKdWqB1ThTP7U9+42KjABgNsnLpVge5xsPQja5QPbvqq+1Krl5z66xTTQQNxMvnKQt1fjUMqaDB52vDYbm8yh8iM4+fDPhbfmoDB5eFqwADjQ3b7kcKYgCF2D56KBxrVVHzFMMR70lkn8q6krZ4Ie9djkfBYAkVTkWSwlxwg70u7mg3O123Ddlcbasikg7FihdE3rRXrV69MkG7XzaxPAhEF0Abtubodoe6B78M8GrGUMGGdS8ad13BOGPTkCo9au8uZO7PjjKr16Hi8z4UAc3j6yFAjwkx6Hc5BgBlrVzdmfeRN6GBEMFkj1X5/OdBDLqSgUQ7t8QQr5GE1p24mMOGSHpvp45EQELJjyqGzJb4SL3CIBN82q0HOryKXE3NUd3PiZK+WanAQKYZfjCn3FpeSnbjXpRu+Fst7oqrv5Zkh4FucwihLqjGfEtwJhJ/gG3mUXqNOTHZ0S03pPuIVo4iiWrBRzY4EeqBDwkqgJCc=';
}


/**
 * Method to get a 256 Bit SHA 256 Private key to give an idea of the format
 * Your key should come from a secure certificate or secret store
 * and NEVER be stored in code or where it is user readable
 */
private String getPrivateKey(){
	String privateKey ='';
	privateKey+='MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDaVmKLA1RFOpv5';
	privateKey+='uCI8QgpeAxpDlkNSxksSn+aJGnLHzBjhAQRLD0skGXk3DH8FQp3ffEX3BC6/M8Ga';
	privateKey+='IF7pAdOt+j3xHv+0+dQ7qpDqk433KKYLJaoI6AfDA5R+CfsJH4RcR0W8kTHF/+SB';
	privateKey+='3aNGq5fDFD5AymIJiQeuJ3GO/r3gbRd5MpC6tI8UzGUQlnKFZvBK0W63nnYkxHEk';
	privateKey+='E7gu/JWoZdfY7UcXEfhWte5DjOwtzq3DsGu4J6peGkYCAOtwCCrkpaumiDe3AqrQ';
	privateKey+='eKoDsT+KDj1qMoD4Tnq1buCKMFQEKHY0KqCeT5IrcHZzsSNYZF4zz2gT6h5N+56G';
	privateKey+='+F281mkHAFzIV582N5P0gh38FCJ/Qg6crRyYKeoxqm12sbxVbGEw0pROMURbHYnc';
	privateKey+='nJlTTFQploVz5ZW8jgO8XEEjWbjkF7IBuu+FCgHFZ3JwoPyMeaAcGPPPRBrJAlR7';
	privateKey+='1sm5f2WelHfJZKBLG8vxOFY9W85v7fMLZztE4yL1+0LiI2W7426ADfJRQ3sO2EA/';
	privateKey+='WyOlTrBrk3Mvsj/9bkQ6C2aIIqNsmnvtQU+hzaZ10tyoIuQiUmHuDWvvKHiLw3EQ';
	privateKey+='c7BLSlZaQkwG66kZUf/OUwsf5u2knywxw3oEbptrhuA8iZ17lQWoxbvkum2an6tA';
	privateKey+='J7cL5yOVEYiq+C/O4gIVAXo7OTfDKwIDAQABAoICAQDXbq8zDSjkWi023EnjfSIv';
	privateKey+='mw4aLDTngsLmcKIPG+qvW6IcuV0cFs4Eo6HoAEuAzDdsIXoDfrwFazMXOeOMM4JD';
	privateKey+='QwxFU1npnyybZvQwkUFd41za2OIKga/O60RkjMKHQPLf/m8/3V/oR1KYFTIa9Ar9';
	privateKey+='sIawdUEAuZW7cC3rc021GM81hgAqSSh41CjQkjITkPt+R2Hgidl28+HTQCXXLuEY';
	privateKey+='VM0CvTUM/W01WbPsMSuFE05/LR9MiM7gpTn4liG43EY+b32MBxI6YMZchwLyO70/';
	privateKey+='IkfU9lotJ3qKdQnXMFJl++qOFJWBP3AxrnkiNGF0wSv2IStHoxUdyy2jbYNIZaMg';
	privateKey+='MmQE+kFhUz0mNaiJqHKoELDT6xEBMxWrfEVt8iQeISplCAh4cBtPUL+xFQ94yIFo';
	privateKey+='CFecmgDk3noUwWu5fbEIkx2ui2mb4aWUN2FICd2KLyJSY/wbtFiKJ4eX49mukGId';
	privateKey+='chNlAymrtVWI0k1GVa3JF+rjGbzJHG/hkxp2ccK4aq3Kl7mmvvYyKVXcgRQ3UQiA';
	privateKey+='EuawNmqXtaTXwWOiBFYG6Gq/GIzsMZAPI2slg3pM0rCR1rJx+EqE8Fd8f4/Ua8WM';
	privateKey+='m3rP50izBEtKWuNNSCopWyWDFH/HRvgKixU71NkLYQ5MHvfcpealH4i7zi3W6aeR';
	privateKey+='5weNmGZ47o5l9xz/JIKpsQKCAQEA//Df6UsDNuCAcyk89d0+AELZ2T0i4xk+7p2M';
	privateKey+='hgPo2iTm5MTgRl01NJ/LmsNJJ813h2dwyV6bMzdx+daziGybbivRJpz2mkCOwSqD';
	privateKey+='E4+7wRjyaYRK8/aBY8KbcTYt6GY+UhBMh01Yg7mRZWSmFo0LL6V6vVVo3irkY6Kh';
	privateKey+='Oy3plW6mTa1LNUMoavqEVyn/v9tYVTsRxiZVtvxjzsfcTuQ+gS1INKs8sTIfY9PV';
	privateKey+='keho+gDaJYn/ZmFb1Tt70zSkQblwAG7nIA0pQtuuUF874FzhH7hUkwuASbjv2D5G';
	privateKey+='5TwXOHbrR+MBrp2XbXSQ074DrCCMuw0xofSeELsaADK9c1p0GQKCAQEA2mNJvB1/';
	privateKey+='TrJY2I/uzILEBl2qo66Sr8o8U0FCl/6/kVWzMIPhZz92+KedFKmJ5DGgFTUWri9F';
	privateKey+='vz8ZxoeUDizikQKG9R26ZxZDJtvlkyKqY28ja9D0Iyb3QhV1qwExO4Fo2XCFgzCY';
	privateKey+='2bqEwMCZ9G9LI3vWE3AOXQi0LdE39ImSRLoYJQmgfxDnoNQeofd3v7mbrwkyKuBv';
	privateKey+='6jB+apeysiXSEsK3c/Xmrc2ceNnwHeZbWJJW+9aMWB0g6kK4fneXOz84F6gaAtU5';
	privateKey+='q5kje02qRJZW8tKGBt07DnflhqAQRuuIIipk5yxCFmnJXUVSyzohoO5QSa53NAS4';
	privateKey+='J7a8t9ivhJt54wKCAQA+IxXJhuut3A1zaBSjwGX4HELVihE5P3zW46slMjfLFmB0';
	privateKey+='NBQbIS/0qcL9vOG65xhY6FUqnmxhn9ltBaIqwetucPbjQAJi6r99yDtweVnlBJB9';
	privateKey+='659i4XsCZFHmx3eXz5Lby5c41h9iQ6A4FJp6KR4JIEzPQLgoEBPI2Mf6HShznhyE';
	privateKey+='CUmUEczzRATzQIAV7UPh1Wh4SgBPX7E/l6g2AxXluL+qAdTHVFromppkRR+ParuL';
	privateKey+='l5hJG+P2ve1PFp22UzYM9N/qGSfmUn1ch/J6gzIoyFDILmej/mFEh8Igj1k33S88';
	privateKey+='EbHr2djUuxMaRSyREon4M7jUEBZ8C2DBoY/7PH+pAoIBAF7wqR6ByFWjDSLgt9Tq';
	privateKey+='yGNoFwXfn9+SUNV02omPcyKwmhzuSHCFU6hX3d8csVimBk0R7lE9NdoliYQYbtIW';
	privateKey+='y0x1R8yJ5v5n3Dupf02O1Xoy17hId8pMZ1OwVp5H/2o6ISXeV/ynhNuqzYmqcYOl';
	privateKey+='WooLjQ6YOXZSkVoVyXii0hbUvChl3gM/iyMM9GA/YCzWeQsIOWoQdjbebCbU2he+';
	privateKey+='f2wugGiGL4nBiFO1k0C8Y1vHCs+i/xJTX3rYFLLONM3J9w25w80Ve2PRSG6TXgFB';
	privateKey+='Rn177k5PoRvyHbOAJHNgc6c+vO0O/ZAW3zaQK6U0GWiIEhlmImZX4uNI+xQFvJAu';
	privateKey+='szcCggEAdOOsFRBiTMWgCZ3Q3vqB6rkzIUiqZ9vwJzJHy3mIoENkQLsjZgKSsJBq';
	privateKey+='Ter+eMX8HaObGbKV7f10RBzJnV6yq/z7BohuUbkQ+an8BWYSnveWgu1eHifyaDVG';
	privateKey+='h7SivmPeJDq6HE0Nh2jehfjpsmZmnUSTk0Lz7FBshT5o7R5T8Od7hXWIZMLBoXih';
	privateKey+='CSjMXI6IbT94nG0110GbvlHkwbbK/TN/7rUA3Afepzq74kKgY5zJP3aj6tq1FlCh';
	privateKey+='8Y5Y9uoB29EfrzSWrRkXjZQhR/yiyIulUV/NZS2KqWMtQGJjw9aeUVLqyglIzS5q';
	privateKey+='SB/8ZTFkXV09anyt4L5j8t6Th1DUdA==';
	return privateKey;
}


/**
 * Method to get the public key
 * Again this is just for illustative purposes, you would NEVER put a key like this in your code
 */
private String getPublicKey(){
	String publicKey ='';
	publicKey+='MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA2lZiiwNURTqb+bgiPEIK';
	publicKey+='XgMaQ5ZDUsZLEp/miRpyx8wY4QEESw9LJBl5Nwx/BUKd33xF9wQuvzPBmiBe6QHT';
	publicKey+='rfo98R7/tPnUO6qQ6pON9yimCyWqCOgHwwOUfgn7CR+EXEdFvJExxf/kgd2jRquX';
	publicKey+='wxQ+QMpiCYkHridxjv694G0XeTKQurSPFMxlEJZyhWbwStFut552JMRxJBO4LvyV';
	publicKey+='qGXX2O1HFxH4VrXuQ4zsLc6tw7BruCeqXhpGAgDrcAgq5KWrpog3twKq0HiqA7E/';
	publicKey+='ig49ajKA+E56tW7gijBUBCh2NCqgnk+SK3B2c7EjWGReM89oE+oeTfuehvhdvNZp';
	publicKey+='BwBcyFefNjeT9IId/BQif0IOnK0cmCnqMaptdrG8VWxhMNKUTjFEWx2J3JyZU0xU';
	publicKey+='KZaFc+WVvI4DvFxBI1m45BeyAbrvhQoBxWdycKD8jHmgHBjzz0QayQJUe9bJuX9l';
	publicKey+='npR3yWSgSxvL8ThWPVvOb+3zC2c7ROMi9ftC4iNlu+NugA3yUUN7DthAP1sjpU6w';
	publicKey+='a5NzL7I//W5EOgtmiCKjbJp77UFPoc2mddLcqCLkIlJh7g1r7yh4i8NxEHOwS0pW';
	publicKey+='WkJMBuupGVH/zlMLH+btpJ8sMcN6BG6ba4bgPImde5UFqMW75Lptmp+rQCe3C+cj';
	publicKey+='lRGIqvgvzuICFQF6Ozk3wysCAwEAAQ==';
	return publicKey;
}
content_copyCOPY