Rev 414 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 415 | espaco | 1 | package nfe.util; |
| 414 | espaco | 2 | |
| 3 | import java.security.MessageDigest; |
||
| 4 | import java.security.NoSuchAlgorithmException; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * |
||
| 8 | * @author Samuel Oliveira |
||
| 9 | */ |
||
| 10 | public class NFCeUtil { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * |
||
| 14 | * Funcao Responsavel por Devolver o QrCode já no padrão da Nota. |
||
| 15 | * |
||
| 16 | * @param chave : Chave de Acesso da NFCe |
||
| 17 | * @param ambiente : Identificação do Ambiente (1 – Produção, 2 – Homologação) |
||
| 18 | * @param idToken : Identificador do CSC – Código de Segurança do Contribuinte no Banco de Dados da SEFAZ |
||
| 19 | * @param CSC : Código de Segurança do Contribuinte (antigo Token) |
||
| 20 | * @param urlConsulta : Url De Consulta da Nfc-e do Estado |
||
| 21 | * |
||
| 22 | * @return String do QrCode |
||
| 23 | */ |
||
| 24 | public static String getCodeQRCode(String chave, String ambiente, String idToken, String CSC, String urlConsulta) throws NoSuchAlgorithmException { |
||
| 25 | |||
| 26 | StringBuilder value = new StringBuilder(); |
||
| 27 | value.append(chave); |
||
| 28 | value.append("|").append("2"); |
||
| 29 | value.append("|").append(ambiente); |
||
| 30 | value.append("|").append(Integer.valueOf(idToken)); |
||
| 31 | String cHashQRCode = getHexa(getHash(value.toString() + CSC)).toUpperCase(); |
||
| 32 | |||
| 33 | return urlConsulta + "?p=" + value + "|" + cHashQRCode; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Funcao Responsavel por Devolver o QrCode já no padrão da Nota. |
||
| 38 | * |
||
| 39 | * @param chave : Chave de Acesso da NFCe |
||
| 40 | * @param ambiente : Identificação do Ambiente (1 – Produção, 2 – Homologação) |
||
| 41 | * @param idToken : Identificador do CSC – Código de Segurança do Contribuinte no Banco de Dados da SEFAZ |
||
| 42 | * @param CSC : Código de Segurança do Contribuinte (antigo Token) |
||
| 43 | * @param urlConsulta : Url De Consulta da Nfc-e do Estado |
||
| 44 | * @return String do QrCode |
||
| 45 | */ |
||
| 46 | public static String getCodeQRCodeContingencia(String chave, String ambiente, String dhEmi, String valorNF, String digVal, String idToken, String CSC, String urlConsulta) throws NoSuchAlgorithmException { |
||
| 47 | |||
| 48 | StringBuilder value = new StringBuilder(); |
||
| 49 | value.append(chave); |
||
| 50 | value.append("|").append("2"); |
||
| 51 | value.append("|").append(ambiente); |
||
| 52 | value.append("|").append(dhEmi, 8, 10); |
||
| 53 | value.append("|").append(valorNF); |
||
| 54 | value.append("|").append(getHexa(digVal)); |
||
| 55 | value.append("|").append(Integer.valueOf(idToken)); |
||
| 56 | String cHashQRCode = getHexa(getHash(value.toString() + CSC)).toUpperCase(); |
||
| 57 | |||
| 58 | return urlConsulta + "?p=" + value + "|" + cHashQRCode; |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * @param valor |
||
| 64 | * @return |
||
| 65 | */ |
||
| 66 | private static byte[] getHash(String valor) throws NoSuchAlgorithmException { |
||
| 67 | |||
| 68 | MessageDigest md = MessageDigest.getInstance("SHA-1"); |
||
| 69 | md.update(valor.getBytes()); |
||
| 70 | return md.digest(); |
||
| 71 | } |
||
| 72 | |||
| 73 | private static String getHexa(String valor) { |
||
| 74 | return getHexa(valor.getBytes()); |
||
| 75 | } |
||
| 76 | |||
| 77 | private static String getHexa(byte[] bytes) { |
||
| 78 | StringBuilder s = new StringBuilder(); |
||
| 79 | for (byte aByte : bytes) { |
||
| 80 | int parteAlta = ((aByte >> 4) & 0xf) << 4; |
||
| 81 | int parteBaixa = aByte & 0xf; |
||
| 82 | if (parteAlta == 0) { |
||
| 83 | s.append('0'); |
||
| 84 | } |
||
| 85 | s.append(Integer.toHexString(parteAlta | parteBaixa)); |
||
| 86 | } |
||
| 87 | return s.toString(); |
||
| 88 | } |
||
| 89 | |||
| 90 | } |
||
| 91 |