Python

Here you'll find a few samples of code to implement the Authentication and perform specific actions in Python.

Code on this page might require third party libraries Soldo is not affiliated with. Use these samples at your discretion.

📘

Make sure to read all articles under Getting Started before proceeding with the implementation.

Advanced authentication

This code sample contains useful functions to implement advanced authentication.

################################################################
#Script Name	: Advanced Authentication Python example       #
#Description	: This script contains functions that generate #
#                 the fingerprint and fingerprint signature by #
#                 reading the private key from an external     #
#                 file, this is just an example STORE YOUR     #
#                 CREDENTIALS SAFELY                           #
################################################################

import base64
import logging

from cryptography.exceptions import AlreadyFinalized
from cryptography.exceptions import UnsupportedAlgorithm
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.asymmetric import rsa


# set up logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# function to be used to generate the hashed fingerprint, input argument is the fingerprint order concatenated string
def fingerprintHash(fingerprint):
    try:
        # get digest instance
        digest = hashes.Hash(
            algorithm=hashes.SHA512(),
            backend=default_backend()
        )

        # create hash
        digest.update(fingerprint.encode('utf-8'))
        hash_bytes = digest.finalize()

        # convert/encode in hex
        return hash_bytes.hex()

    except (UnsupportedAlgorithm, AlreadyFinalized):
        logger.exception("Hashing failed")

# function to be used to generate the fingerprint signature, input arguments are the hashed fingerprint to be signed and the private 2048 RSA key file path
def fingerprintSignature(fingerprintH, keyPath):
    try:
        # load private key
        with open(keyPath, "rb") as key_file:
            privateKey = serialization.load_pem_private_key(
                key_file.read(),
                password=None,
                backend=default_backend()
            )

        # sign string
        signature = privateKey.sign(
            data=fingerprintH.encode('utf-8'),
            padding=padding.PKCS1v15(),
            algorithm=hashes.SHA512()
        )

        return base64.b64encode(signature)
    except UnsupportedAlgorithm:
        logger.exception("Signing failed")

if __name__ == '__main__':

    keyPath = "C:\\Users\\user\\Desktop\\privateKey.pem"

    # fingerprint example for Internal transfer, fingerpint order: amount, currencyCode, fromWalletId, toWalletId, token (http://apidoc-demo.soldo.com/v2/zgxiaxtcyapyoijojoef.html#internal-transfer)
    fingerprint = "300EUR764674e4-8a94-47cc-b5ad-2430aba5aaa697191a41-9603-46f1-b751-e44ebda1bb9d4UH8WMO8BVLFNHUXFB9E"
    logger.info("Fingerprint: %s", fingerprint)

    # fingerprint hash for X-Soldo-Fingerprint header
    fingerprintH = fingerprintHash(fingerprint)
    logger.info("Hash: %s", fingerprintH)

    # fingerprint hash signature for X-Soldo-Fingerprint-Signature header
    fingerprintS = fingerprintSignature(fingerprintH, keyPath)
    logger.info("Signature: %s", fingerprintS)