List of usage examples for javax.xml.bind DatatypeConverter printHexBinary
public static String printHexBinary(byte[] val)
Converts an array of bytes into a string.
From source file:net.pot8os.heartbleedchecker.HexUtil.java
public static String printBytes(byte[] input) { return DatatypeConverter.printHexBinary(input); }
From source file:com.omnigon.aem.handlebars.helpers.UniqueId.java
public static String generateUniqueId(String directoryPath) { byte[] bytesDirectoryPath = null; MessageDigest md = null;//from w ww. j av a 2 s . com try { bytesDirectoryPath = directoryPath.getBytes(CharEncoding.UTF_8); md = MessageDigest.getInstance(MESSAGE_DIGEST_TYPE); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { logger.error(e.getMessage(), e); return StringUtils.EMPTY; } md.reset(); md.update(bytesDirectoryPath); String uniqueId = DatatypeConverter.printHexBinary(md.digest()); return StringUtils.substring(uniqueId, 0, UNIQUE_ID_LENGTH); }
From source file:at.tuwien.mnsa.smssender.SMSPDUConverterTest.java
/** * Test of getPDU method, of class SMSPDUConverter. *//*from w w w . j a v a 2s. c o m*/ @org.junit.Test public void testGetPDU() { SMS s1 = new SMS("436604678660", "$$$"); byte[] b1 = SMSPDUConverter.getInstance().getContent(s1.getText()).message; assertEquals("028100", DatatypeConverter.printHexBinary(b1)); SMS s = new SMS("+436509030464", "SMS Rulz"); byte[] b = SMSPDUConverter.getInstance().getContent(s.getText()).message; assertEquals("D3E61424ADB3F5", DatatypeConverter.printHexBinary(b)); s = new SMS("436604678660", "lo"); b = SMSPDUConverter.getInstance().getContent(s.getText()).message; assertEquals("EC37", DatatypeConverter.printHexBinary(b)); s = new SMS("436604678660", "hellohello"); b = SMSPDUConverter.getInstance().getContent(s.getText()).message; assertEquals("E8329BFD4697D9EC37", DatatypeConverter.printHexBinary(b)); }
From source file:br.com.topsys.cd.util.AssinaturaDigital.java
public String assinar(CertificadoDigital certificadoDigital, String hash) throws CertificadoDigitalException { return DatatypeConverter.printHexBinary(assinar(certificadoDigital, hash.getBytes())); }
From source file:at.tuwien.mnsa.smssender.SMSPDUConverterTest.java
@Test public void testLengths() { String msg, ret;/*from ww w . j av a 2 s . com*/ msg = "1"; ret = DatatypeConverter.printHexBinary(SMSPDUConverter.getInstance().getContent(msg).message); assertEquals(SMSPDUConverter.getInstance().getContent(msg).len, 1); assertEquals(ret, "31"); msg = "12"; ret = DatatypeConverter.printHexBinary(SMSPDUConverter.getInstance().getContent(msg).message); assertEquals(SMSPDUConverter.getInstance().getContent(msg).len, 2); assertEquals(ret, "3119"); msg = "123"; ret = DatatypeConverter.printHexBinary(SMSPDUConverter.getInstance().getContent(msg).message); assertEquals(SMSPDUConverter.getInstance().getContent(msg).len, 3); assertEquals(ret, "31D90C"); msg = "1234"; ret = DatatypeConverter.printHexBinary(SMSPDUConverter.getInstance().getContent(msg).message); assertEquals(SMSPDUConverter.getInstance().getContent(msg).len, 4); assertEquals(ret, "31D98C06"); msg = "12345"; ret = DatatypeConverter.printHexBinary(SMSPDUConverter.getInstance().getContent(msg).message); assertEquals(SMSPDUConverter.getInstance().getContent(msg).len, 5); assertEquals(ret, "31D98C5603"); msg = "123456"; ret = DatatypeConverter.printHexBinary(SMSPDUConverter.getInstance().getContent(msg).message); assertEquals(SMSPDUConverter.getInstance().getContent(msg).len, 6); assertEquals(ret, "31D98C56B301"); msg = "1234567"; ret = DatatypeConverter.printHexBinary(SMSPDUConverter.getInstance().getContent(msg).message); assertEquals(SMSPDUConverter.getInstance().getContent(msg).len, 7); assertEquals(ret, "31D98C56B3DD00"); msg = "12345678"; ret = DatatypeConverter.printHexBinary(SMSPDUConverter.getInstance().getContent(msg).message); assertEquals(SMSPDUConverter.getInstance().getContent(msg).len, 8); assertEquals(ret, "31D98C56B3DD70"); }
From source file:com.hviper.codec.uodecode.PeakSignalNoiseRatioTest.java
private double computePsnrPcm16(String uoResource, String referenceResource) throws Exception { // Read the UO file and the reference encoder's decode output into byte arrays byte uoFile[] = IOUtils.toByteArray(this.getClass().getResourceAsStream(uoResource)); byte referenceWavFile[] = IOUtils.toByteArray(this.getClass().getResourceAsStream(referenceResource)); // Decode the UO file ourselves into a byte array ByteArrayOutputStream decodedWavOutputStream = new ByteArrayOutputStream(referenceWavFile.length); UODecode.uoToPcm16Wav(uoFile, decodedWavOutputStream); byte decodedWavFile[] = decodedWavOutputStream.toByteArray(); // Find the start of the sample data; this will be after a 'data' header and four bytes of // content length. int dataStart = -1; for (int i = 0; i < decodedWavFile.length - 4; ++i) { if ((decodedWavFile[i] == 'd') && (decodedWavFile[i + 1] == 'a') && (decodedWavFile[i + 2] == 't') && (decodedWavFile[i + 3] == 'a')) { dataStart = i + 8; // 8 = length of header + chunk length break; }//from w w w. j av a 2 s. c om } assertFalse("No 'data' header in decoded output", dataStart < 0); // Headers must be equal. Compare as hex strings for better assert failures here. String refHeaders = DatatypeConverter.printHexBinary(Arrays.copyOfRange(referenceWavFile, 0, dataStart)); String ourHeaders = DatatypeConverter.printHexBinary(Arrays.copyOfRange(decodedWavFile, 0, dataStart)); assertEquals("WAV headers do not match", refHeaders, ourHeaders); assertEquals("File lengths do not match", referenceWavFile.length, decodedWavFile.length); // Compute total squared error int cursor = dataStart; long totalSqError = 0; int sampleCount = 0; int worstSoFar = 0; for (; (cursor + 1) < referenceWavFile.length; cursor += 2) { short refSample = (short) ((referenceWavFile[cursor] & 0xff) | ((referenceWavFile[cursor + 1] & 0xff) << 8)); short ourSample = (short) ((decodedWavFile[cursor] & 0xff) | ((decodedWavFile[cursor + 1] & 0xff) << 8)); int absDiff = Math.abs(ourSample - refSample); long sqError = ((long) absDiff) * ((long) absDiff); totalSqError += sqError; ++sampleCount; } assertNotEquals("No samples read!", 0, sampleCount); // Compute the PSNR in decibels; higher the better double psnr; if (totalSqError > 0) { double sqrtMeanSquaredError = Math.sqrt((double) (totalSqError) / (double) (sampleCount)); double maxValue = 65535.0; psnr = 20.0 * Math.log10(maxValue / sqrtMeanSquaredError); } else { // Identical! Pick a large PSNR result psnr = 1000.0; } return psnr; }
From source file:com.amazon.proposalcalculator.reader.StandByInstances.java
private static String bytesToHex(byte[] hash, int size) { return DatatypeConverter.printHexBinary(hash).substring(0, size); }
From source file:cz.cas.lib.proarc.common.imports.InputUtils.java
private static boolean startWith(byte[] src, int srcLength, byte[] subarray) { if (subarray.length > srcLength) { return false; }/*from ww w . jav a 2 s .co m*/ StringBuilder s1 = new StringBuilder(); StringBuilder s2 = new StringBuilder(); for (int i = 0; i < subarray.length; i++) { s1.append(src[i]); s2.append(subarray[i]); if (src[i] != subarray[i]) { if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "\nsrc: {0}\nsub: {1}", new Object[] { DatatypeConverter.printHexBinary(src), DatatypeConverter.printHexBinary(subarray) }); } return false; } } return true; }
From source file:at.tuwien.mnsa.smssender.SMS.java
/** * Get the PDU representation of a single SMS message (<= 160 chars) * @return //w w w. j a va 2s .c o m */ public SMSRepresentation getMessage() { //https://en.wikipedia.org/wiki/GSM_03.40 //https://stackoverflow.com/questions/12298558/how-to-build-concatenated-sms-pdu-getting-junk-chars //http://mobiletidings.com/2009/02/18/combining-sms-messages/ List<Byte> bytes = new ArrayList<>(); bytes.add((byte) 0x00); //SMSC Information bytes.add((byte) 0x11); //First octed of the SMS-Submit message bytes.add((byte) 0x00); //TP-Message-Reference (0: phone default) int lenRec = this.recipient.length(); bytes.add((byte) lenRec); bytes.add((byte) 0x91); bytes.addAll(Arrays.asList(ArrayUtils.toObject(getSemiOctet(this.recipient)))); bytes.add((byte) 0x00); //TP-PID Protocol Identifier bytes.add((byte) 0x00); //7 bit encoding bytes.add((byte) 0xAA); //validity (4 days) - @TODO: optional? SMSPDUConverter.SMSPDUConversionResult result = SMSPDUConverter.getInstance().getContent(this.text); int lenContent = result.len; //count of septets bytes.add((byte) lenContent); byte[] message = result.message; bytes.addAll(Arrays.asList(ArrayUtils.toObject(message))); String sMessage = DatatypeConverter .printHexBinary(ArrayUtils.toPrimitive(bytes.toArray(new Byte[bytes.size()]))); int smsLen = (sMessage.length() / 2) - 1; SMSRepresentation r = new SMSRepresentation(sMessage, smsLen); return r; }
From source file:API.Start.java
private boolean isAuthorized(String username, String password) { boolean isAuthorized = false; String modifiedPw = password; try {/*w w w .j av a2 s .co m*/ PreparedStatement query = con.prepareStatement( "SELECT sha(concat(username, rand())) as token, password, salt from user WHERE username = ?"); query.setString(1, username); ResultSet rs = query.executeQuery(); if (rs.next()) { String salt = rs.getString(3); modifiedPw = modifiedPw.concat(salt); MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(modifiedPw.getBytes(StandardCharsets.UTF_8)); String hashPass = DatatypeConverter.printHexBinary(hash); if (hashPass.equals(rs.getString(2))) { token = rs.getString(1); setToken(token, username); isAuthorized = true; } } /*******/ /* - identify if token is valid - if valid get type and location id */ /* boolean isAuthorized = false; String sql = "select sha(concat(username, rand())) as token\n" + "from user \n" + "where username = ? and password = ?;"; try { PreparedStatement s = con.prepareStatement(sql); s.setString(1, username); s.setString(2, password); ResultSet rs = s.executeQuery(); if(rs.next()) { token = rs.getString(1); setToken(token, username, password); isAuthorized = true; } } catch (SQLException ex) { Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex); } return isAuthorized; */ } catch (SQLException ex) { Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex); } return isAuthorized; }