Skip to Content

Extract key from certificate file in SAP UI5

Recently i came across a use case in my product where I have to accept a .CER file from User and pass the certificate to the backend services.

Due to some reason we were unable to handle the certificate file in backend so from UI side we have to extract the key from .CER file and pass it to the backend.

I started with a Google Search but didn’t got any solution for certificate transcription using Javascript. So I just gave a try by writing Javascript logic to extract string from text file (.txt) , and surprisingly i was able to extract the key from certificate using the same logic with some additional string formatter code.

To start with add FileUploader in your view

	<FileUploader
	id="fileUploader"
	name="myFileUpload"
	width="400px"
	tooltip="Upload .cer file"
	change="handleUploadComplete"/>

 

Add the following code in the controller

 

handleUploadComplete: function(oEvent) {

    var file = oEvent.getParameter("files") && oEvent.getParameter("files")[0];
    if (file && window.FileReader) {
        var reader = new FileReader(); 
        reader.onload = function(evn) {
            var certificate = evn.target.result; //string in CER
          	
          //Now lets format the complete string, so that the key is in single line
            certificate = certificate.split("\n");

            formated_certificate = certificate.slice(1, certificate.length - 2).join("");


        };
        reader.readAsText(file);
    }

}

 

Done!!

Be the first to leave a comment
You must be Logged on to comment or reply to a post.