Advertisement
ballchaichana

signtureutil

Nov 9th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.10 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package th.in.oneauthen.signing;
  7.  
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfReader;
  10.  
  11. import java.io.ByteArrayInputStream;
  12. import java.io.ByteArrayOutputStream;
  13. import java.util.Arrays;
  14. import java.util.Date;
  15.  
  16. import th.in.oneauthen.object.LogReportDB;
  17. import th.in.oneauthen.object.UserUidDB;
  18. import th.in.oneauthen.object.DAO.LogReportDAO;
  19. import th.teda.ETSITS102778.PAdES;
  20. import th.teda.ETSITS102778.PAdESSignatureAppearance;
  21.  
  22. /**
  23.  *
  24.  * @author paradorn
  25.  */
  26. public class SignatureUtil {
  27.    
  28.     private final SignatureProfile profile;
  29.     private String timestampURL = "";
  30.     public static final String ALGORITHM = "sha256withrsa";
  31.    
  32.     public SignatureUtil(SignatureProfile profile, String timestampURL){
  33.         this.profile = profile;
  34.         this.timestampURL = timestampURL;
  35.     }
  36.    
  37.     public byte[] SignTheDocument(byte[] pdfData){
  38.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  39.         try {
  40.             PAdESSignatureAppearance sap=null;
  41.             if (profile.isIsVisible()){
  42.                 PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfData));
  43.                 sap = new PAdESSignatureAppearance(reader);
  44.                 sap.setCertifiedLevel(PAdESSignatureAppearance.PAdESCertifiedLevel.CERTIFIED);
  45.                 float[] sigCoordinate = this.tranformSignatureCoordinate(reader.getPageSize(1), profile.getSignatuerCoOrdinate());
  46. //                System.out.println(Arrays.toString(sigCoordinate));
  47.                 if (profile.getSigImg()!=null) {
  48.                     sap.setImage(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getSigImg(), profile.getImagePage());
  49.                 } else {
  50.                     sap.setSignerInfoAppearance(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getImagePage());
  51.                 }
  52.             }
  53.            
  54.             PAdES.sign(profile.getPk(), profile.getCertificateChain(), new ByteArrayInputStream(pdfData), baos, ALGORITHM, sap, this.timestampURL , null,null,null);
  55.             return baos.toByteArray();
  56.         } catch (Exception e) {
  57.             e.printStackTrace();
  58.         }
  59.         return null;
  60.     }
  61.    
  62.     public byte[] SignTheDocumentAPI(byte[] pdfData, UserUidDB user, int sigProfile_id){
  63.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  64.         try {
  65.             PAdESSignatureAppearance sap=null;
  66.             if (profile.isIsVisible()){
  67.                 PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfData));
  68.                 sap = new PAdESSignatureAppearance(reader);
  69.                 sap.setCertifiedLevel(PAdESSignatureAppearance.PAdESCertifiedLevel.CERTIFIED);
  70.                 float[] sigCoordinate = this.tranformSignatureCoordinate(reader.getPageSize(1), profile.getSignatuerCoOrdinate());
  71. //                System.out.println(Arrays.toString(sigCoordinate));
  72.                 if (profile.getSigImg()!=null) {
  73.                     sap.setImage(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getSigImg(), profile.getImagePage());
  74.                 } else {
  75.                     sap.setSignerInfoAppearance(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getImagePage());
  76.                 }
  77.             }
  78.            
  79.             PAdES.sign(profile.getPk(), profile.getCertificateChain(), new ByteArrayInputStream(pdfData), baos, ALGORITHM, sap, this.timestampURL , null,null,null);
  80.             return baos.toByteArray();
  81.         } catch (Exception e) {
  82.             e.printStackTrace();
  83.             String issue = e.toString();
  84.             LogReportDAO saveLogDao = new LogReportDAO();
  85.             LogReportDB saveLog = new LogReportDB();
  86.  
  87.             saveLog.setCreator(user);
  88.             saveLog.setTimeSign(new Date());
  89.             saveLog.setStatus("fail");
  90.             saveLog.setTotal(1);
  91.             saveLog.setType("ApiResful");
  92.             saveLog.setIssue(issue);
  93.             saveLog.setSignatureId(sigProfile_id);
  94.  
  95.                 try {
  96.                     saveLogDao.save(saveLog);
  97.                 } catch (Exception e1) {
  98.                     // TODO Auto-generated catch block
  99.                     e1.printStackTrace();
  100.                 }
  101.    
  102.         }
  103.         return null;
  104.     }
  105.    
  106.     public byte[] SignTheDocumentAPIforOne(byte[] pdfData, UserUidDB user, int sigProfile_id,String service_name){
  107.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  108.         try {
  109.             PAdESSignatureAppearance sap=null;
  110.             if (profile.isIsVisible()){
  111.                 PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfData));
  112.                 sap = new PAdESSignatureAppearance(reader);
  113.                 sap.setCertifiedLevel(PAdESSignatureAppearance.PAdESCertifiedLevel.CERTIFIED);
  114.                 float[] sigCoordinate = this.tranformSignatureCoordinate(reader.getPageSize(1), profile.getSignatuerCoOrdinate());
  115. //                System.out.println(Arrays.toString(sigCoordinate));
  116.                 if (profile.getSigImg()!=null) {
  117.                     sap.setImage(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getSigImg(), profile.getImagePage());
  118.                 } else {
  119.                     sap.setSignerInfoAppearance(sigCoordinate[0], sigCoordinate[2], sigCoordinate[1], sigCoordinate[3], profile.getImagePage());
  120.                 }
  121.             }
  122.            
  123.             PAdES.sign(profile.getPk(), profile.getCertificateChain(), new ByteArrayInputStream(pdfData), baos, ALGORITHM, sap, this.timestampURL , null,null,null);
  124.             return baos.toByteArray();
  125.         } catch (Exception e) {
  126.             e.printStackTrace();
  127.             String issue = e.toString();
  128.             LogReportDAO saveLogDao = new LogReportDAO();
  129.             LogReportDB saveLog = new LogReportDB();
  130.  
  131.             saveLog.setCreator(user);
  132.             saveLog.setTimeSign(new Date());
  133.             saveLog.setStatus("fail");
  134.             saveLog.setTotal(1);
  135.             saveLog.setType("ApiResful");
  136.             saveLog.setIssue(issue);
  137.             saveLog.setSignatureId(sigProfile_id);
  138.             saveLog.setTypeService(service_name);
  139.  
  140.                 try {
  141.                     saveLogDao.save(saveLog);
  142.                 } catch (Exception e1) {
  143.                     // TODO Auto-generated catch block
  144.                     e1.printStackTrace();
  145.                 }
  146.    
  147.         }
  148.         return null;
  149.     }
  150.    
  151.     public float[] tranformSignatureCoordinate (Rectangle page, float[] percentageCoordinate) {
  152.         float[] signatureCoordinate = new float[4];
  153.         float pageH = (float) page.getHeight();
  154.         float pageW = (float) page.getWidth();
  155.         //llx
  156.         signatureCoordinate[0] = (float) (pageW * percentageCoordinate[0] ); // page sig x0 value
  157.         //urx
  158.         signatureCoordinate[1] = (float) (pageW * (percentageCoordinate[0] + percentageCoordinate[1]) ); // page sig x1 value
  159.         //lly
  160.         signatureCoordinate[2] = (float) (pageH * (1-(percentageCoordinate[2]+percentageCoordinate[3]))); // page sig y0 value
  161.         //ury
  162.         signatureCoordinate[3] = (float) (pageH * (1-percentageCoordinate[2])); // page sig y1 value
  163.        
  164.         return signatureCoordinate;
  165.     }
  166.    
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement