Jump to content
Rui Patronillo

Assinatura faturas (saft)

Recommended Posts

Rui Patronillo

 

Boas voltei.

Para quem precisar assinatura de documentos em java (Android Studio).

Citação

private String Assinar_Documento(String Assinatura)  {
        String Hash="";
        char LineFeed=10;
        String Ass;
        PrivateKey privateKey;
        //String privateKeyString = ""+
        //      "-----BEGIN RSA PRIVATE KEY-----"+
                String privateKeyString =
        "MIIC------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
                "----------------------------------------------------------------"+
        "--------------------------------------------";
                //"-----END RSA PRIVATE KEY-----";
        try {
            privateKey=getPrivateKey(privateKeyString);
            Signature sig = Signature.getInstance("SHA1WithRSA");
            sig.initSign(privateKey);
            byte[] data = Assinatura.getBytes();
            sig.initSign(privateKey);
            sig.update(data);
            byte[] signatureBytes = sig.sign();
            Ass=Base64.encodeToString(signatureBytes,Base64.DEFAULT);
            char chrs[] = Ass.toCharArray();
            int i = 0;
            while (i < chrs.length) {
                if (chrs = LineFeed) {
                    Hash=Hash+chrs;
                }
                i++;
            }
            //System.out.println(Hash);
            return Hash;
        }
        catch (Exception Erro){
            System.out.println(Erro.getMessage());
        }
        return Hash;
    }
    public PrivateKey getPrivateKey(String key) {
        PrivateKey privateKey = null;
        try {
            String privateString = key;
            if(privateString!=null){
                byte[] binCpk = Base64.decode(privateString,Base64.DEFAULT);
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(binCpk);
                privateKey = keyFactory.generatePrivate(privateKeySpec);
            }
        }
        catch(Exception e){
        }
        return privateKey;
    }

 

Edited by Rui Patronillo

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.