Esempio n. 1
0
 @SneakyThrows(NoSuchAlgorithmException.class)
 public DependencyLoader downloadDependencies(DependencySet dependencySet) throws IOException {
   initDependencyStore();
   for (Dependency dependency : dependencySet.getDependencies()) {
     URL cached = dependencyStore.getCachedDependency(dependency);
     if (cached == null) {
       logger.info("Downloading " + dependency.getUrl());
       MessageDigest digest = MessageDigest.getInstance("SHA-512");
       @SuppressWarnings("resource")
       ByteArrayOutputStream memoryStream = new ByteArrayOutputStream();
       try (InputStream stream = dependency.getUrl().openStream()) {
         byte[] buf = new byte[4096];
         int len;
         while ((len = stream.read(buf)) >= 0) {
           digest.update(buf, 0, len);
           memoryStream.write(buf, 0, len);
         }
       }
       byte[] hash = digest.digest();
       if (!Arrays.equals(hash, dependency.getSha512sum())) {
         throw new IOException(
             "Mismatched hash on "
                 + dependency.getUrl()
                 + ": expected "
                 + DatatypeConverter.printHexBinary(dependency.getSha512sum())
                 + " but got "
                 + DatatypeConverter.printHexBinary(hash));
       }
       cached = dependencyStore.saveDependency(dependency, memoryStream.toByteArray());
     }
     urls.add(cached);
   }
   logger.info("Added " + dependencySet.getDependencies().size() + " dependencies.");
   return this;
 }
Esempio n. 2
0
 @Test
 public void test_generate_ARPA() {
   // NetworkInBytes networkInBytes = IpUtil.parseArpa("f.f.ip6.arpa");
   NetworkInBytes networkInBytes =
       ArpaUtil.parseArpa("1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa");
   String hex1 = DatatypeConverter.printHexBinary(networkInBytes.getStartAddress());
   // System.err.println("0x" + hex1);
   String hex2 = DatatypeConverter.printHexBinary(networkInBytes.getEndAddress());
   // System.err.println("0x" + hex2);
 }
Esempio n. 3
0
  public String disassemble8080p(byte[] codeBuffer, int progCounter, int end, boolean hexDump) {
    int pc = progCounter;
    String all = "";

    while (pc <= end) {
      byte c = codeBuffer[pc];

      OPEntry op = getOPDataFromCode(c);

      String finalLine = String.format("%04X ", pc);

      if (op.bytes == 1)
        finalLine += DatatypeConverter.printHexBinary(new byte[] {codeBuffer[pc]}) + "       ";
      else if (op.bytes == 2)
        finalLine +=
            DatatypeConverter.printHexBinary(new byte[] {codeBuffer[pc]})
                + " "
                + DatatypeConverter.printHexBinary(new byte[] {codeBuffer[pc + 1]})
                + "    ";
      else if (op.bytes == 3)
        finalLine +=
            DatatypeConverter.printHexBinary(new byte[] {codeBuffer[pc]})
                + " "
                + DatatypeConverter.printHexBinary(new byte[] {codeBuffer[pc + 1]})
                + " "
                + DatatypeConverter.printHexBinary(new byte[] {codeBuffer[pc + 2]})
                + " ";

      finalLine += op.name;

      if (op.bytes == 2)
        finalLine += "#0x" + DatatypeConverter.printHexBinary(new byte[] {codeBuffer[pc + 1]});
      else if (op.bytes == 3)
        finalLine +=
            "$"
                + DatatypeConverter.printHexBinary(
                    new byte[] {codeBuffer[pc + 2], codeBuffer[pc + 1]});

      all += finalLine + ((pc + op.bytes <= end || hexDump) ? "\n" : "");
      pc += op.bytes;
    }

    if (hexDump) {
      int lines = (int) Math.ceil((end - progCounter) / 0x10);
      if (lines == 0) lines++;

      for (int i = 0; i <= lines; i++) {
        all += String.format("%08X ", progCounter + i * 0x10);
        for (int j = 0; j < 0x10; j++)
          all +=
              " "
                  + DatatypeConverter.printHexBinary(
                      new byte[] {codeBuffer[progCounter + i * 0x10 + j]});
        all += "\n";
      }
    }

    return all;
  }
  @Test
  public void testCborSerialize() throws Exception {
    CBORAccessRequestMac instance = new CBORAccessRequestMac(this.publisherId, this.ifMapTimestamp);

    CborBuilder cb = new CborBuilder();
    ArrayBuilder ab = cb.addArray();

    instance.cborSerialize(ab);

    ab.end();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    CborEncoder ce = new CborEncoder(bos);
    ce.encode(cb.build());

    log.info("CBOR serialize:");
    log.info(DatatypeConverter.printHexBinary(bos.toByteArray()));

    byte[] expResult =
        DatatypeConverter.parseHexBinary(
            "847839687474703A2F2F7777772E74727573746564636F6D707574696E6767726F75702E6F72672F32303130"
                + "2F49464D41504D455441444154412F32726163636573732D726571756573742D6D6163887269666D61702D7075626C69736865722D69646F6D792D7075626C6973"
                + "6865722D69646F69666D61702D74696D657374616D70C11A4ED9E8B2781869666D61702D74696D657374616D702D6672616374696F6EC482281A075BCA00716966"
                + "6D61702D63617264696E616C6974796B73696E676C6556616C756580");

    assertTrue(Arrays.equals(expResult, bos.toByteArray()));
  }
Esempio n. 5
0
 /** Retrieve the generated salt value as a (printable) String. */
 public static String byteArrayToString(byte[] byteArray) {
   String result = ""; // default value if input is null
   // check for null, as IllegalArgumentException could be thrown
   if (byteArray != null) {
     result = DatatypeConverter.printHexBinary(byteArray);
   }
   return result;
 }
  @Override
  public State convertToState(RFXComValueSelector valueSelector) throws RFXComException {

    org.openhab.core.types.State state = UnDefType.UNDEF;

    if (valueSelector.getItemClass() == NumberItem.class) {

      if (valueSelector == RFXComValueSelector.SIGNAL_LEVEL) {

        state = new DecimalType(signalLevel);

      } else if (valueSelector == RFXComValueSelector.BATTERY_LEVEL) {

        state = new DecimalType(batteryLevel);

      } else {
        throw new RFXComException("Can't convert " + valueSelector + " to NumberItem");
      }

    } else if (valueSelector.getItemClass() == RollershutterItem.class) {

      if (valueSelector == RFXComValueSelector.COMMAND) {

        switch (command) {
          case CLOSE:
            state = OpenClosedType.CLOSED;
            break;

          case OPEN:
            state = OpenClosedType.OPEN;
            break;

          default:
            break;
        }

      } else {
        throw new NumberFormatException("Can't convert " + valueSelector + " to RollershutterItem");
      }

    } else if (valueSelector.getItemClass() == StringItem.class) {

      if (valueSelector == RFXComValueSelector.RAW_DATA) {

        state = new StringType(DatatypeConverter.printHexBinary(rawMessage));

      } else {
        throw new NumberFormatException("Can't convert " + valueSelector + " to StringItem");
      }

    } else {

      throw new NumberFormatException(
          "Can't convert " + valueSelector + " to " + valueSelector.getItemClass());
    }

    return state;
  }
Esempio n. 7
0
  public String toString() {
    String str = "";

    str += "Raw data = " + DatatypeConverter.printHexBinary(rawMessage);
    str += "\n - Packet type = " + packetType;
    str += "\n - Seq number = " + (short) (seqNbr & 0xFF);

    return str;
  }
Esempio n. 8
0
 public static String generateHash(String password, String salt) {
   try {
     MessageDigest digest = MessageDigest.getInstance("SHA-256");
     byte[] hash = digest.digest((password + salt).getBytes());
     return DatatypeConverter.printHexBinary(hash);
   } catch (NoSuchAlgorithmException e) {
     return null;
   }
 }
Esempio n. 9
0
 /**
  * 加密
  *
  * @param input 输入字符串
  * @return hex形式的加密数据
  * @throws AESFailedException AES加密失败
  */
 public static String encrypt(String input) throws AESFailedException {
   try {
     byte[] output = encrypt(SEED, input.getBytes("UTF-8"));
     return DatatypeConverter.printHexBinary(output);
   } catch (UnsupportedEncodingException e) {
     // 不可能出现在这里
     throw new AESFailedException(e);
   }
 }
Esempio n. 10
0
 public String md5(String inbuf) {
   try {
     MessageDigest md5 = MessageDigest.getInstance("MD5");
     md5.update(inbuf.getBytes());
     byte[] buf = md5.digest();
     return DatatypeConverter.printHexBinary(buf);
   } catch (Exception ex) {
     return null;
   }
 }
Esempio n. 11
0
  @Override
  public String toString() {
    String str = "";

    str += super.toString();
    str += "\n - Sub type = " + subType;
    str += "\n - Id = " + generateDeviceId();
    str += "\n - Data = " + DatatypeConverter.printHexBinary(rawData);

    return str;
  }
Esempio n. 12
0
  @Override
  public State convertToState(RFXComValueSelector valueSelector) throws RFXComException {

    org.openhab.core.types.State state = UnDefType.UNDEF;

    if (valueSelector.getItemClass() == StringItem.class) {
      if (valueSelector == RFXComValueSelector.RAW_DATA) {
        state = new StringType(DatatypeConverter.printHexBinary(rawMessage));
      } else if (valueSelector == RFXComValueSelector.DATA) {
        state = new StringType(DatatypeConverter.printHexBinary(rawData));
      } else {
        throw new RFXComException("Can't convert " + valueSelector + " to StringItem");
      }
    } else {
      throw new RFXComException(
          "Can't convert " + valueSelector + " to " + valueSelector.getItemClass());
    }

    return state;
  }
  @Test
  public void shouldCalculateCorrectMd5sumForSingleFile() throws Exception {
    OutputStream out = fileIndexFileStrategy.getWriter(mainContainer, "md5sums", writeStrategy);
    InputStream in = getClass().getResourceAsStream("/siarddk/text.tif");
    IOUtils.copy(in, out);
    in.close();
    out.close();

    byte[] digest = fileIndexFileStrategy.addFile("doesNotMatter");
    String md5sum = DatatypeConverter.printHexBinary(digest).toLowerCase();
    assertEquals("a953767181ab088ee22ec3c4d1c45c87", md5sum);
  }
Esempio n. 14
0
  public static void main(String[] args) {
    for (String filename : args) {
      ReturnDigest dr = new ReturnDigest(filename);
      dr.start();

      StringBuilder result = new StringBuilder(filename);
      result.append(": ");
      byte[] digest = dr.getDigest();
      result.append(DatatypeConverter.printHexBinary(digest));
      System.out.println(result);
    }
  }
  @Override
  public State convertToState(RFXComValueSelector valueSelector) throws RFXComException {

    org.openhab.core.types.State state = UnDefType.UNDEF;

    if (valueSelector.getItemClass() == NumberItem.class) {

      if (valueSelector == RFXComValueSelector.SIGNAL_LEVEL) {

        state = new DecimalType(signalLevel);

      } else if (valueSelector == RFXComValueSelector.BATTERY_LEVEL) {

        state = new DecimalType(batteryLevel);

      } else if (valueSelector == RFXComValueSelector.CHANNEL1_AMPS) {

        state = new DecimalType(channel1Amps);

      } else if (valueSelector == RFXComValueSelector.CHANNEL2_AMPS) {

        state = new DecimalType(channel2Amps);

      } else if (valueSelector == RFXComValueSelector.CHANNEL3_AMPS) {

        state = new DecimalType(channel3Amps);

      } else if (valueSelector == RFXComValueSelector.TOTAL_USAGE) {

        state = new DecimalType(totalUsage);

      } else {

        throw new RFXComException("Can't convert " + valueSelector + " to NumberItem");
      }

    } else if (valueSelector.getItemClass() == StringItem.class) {

      if (valueSelector == RFXComValueSelector.RAW_DATA) {

        state = new StringType(DatatypeConverter.printHexBinary(rawMessage));
      } else {
        throw new RFXComException("Can't convert " + valueSelector + " to StringItem");
      }
    } else {

      throw new RFXComException(
          "Can't convert " + valueSelector + " to " + valueSelector.getItemClass());
    }

    return state;
  }
Esempio n. 16
0
 private static final String PASSWORD_DIGEST(String password) {
   try {
     MessageDigest algorithm = MessageDigest.getInstance(PASSWORD_DIGEST_ALGORITHM);
     algorithm.update(PASSWORD_SALT);
     return DatatypeConverter.printHexBinary(algorithm.digest(password.getBytes(PASSWORD_CHARSET)))
         .toUpperCase();
   } catch (NoSuchAlgorithmException exception) {
     // MD5, SHA-1, and SHA-256 are required to be supported in every JRE, as long as we use one of
     // them, this won't happen.
     throw new AssertionError(
         "message digest algorithm " + PASSWORD_DIGEST_ALGORITHM + " is not available");
   }
 }
  @Test
  public void convertFromStateItMessage() throws RFXComException {
    RFXComMessageInterface itMessageObject = RFXComMessageFactory.getMessageInterface(LIGHTING5);
    itMessageObject.convertFromState(
        RFXComValueSelector.COMMAND, "2061.1", IT, OnOffType.ON, (byte) 0);

    byte[] message = itMessageObject.decodeMessage();
    String hexMessage = DatatypeConverter.printHexBinary(message);

    assertEquals("Message is not as expected", "0A140F0000080D01010000", hexMessage);

    RFXComLighting5Message msg =
        (RFXComLighting5Message) RFXComMessageFactory.getMessageInterface(message);
    assertEquals("SubType", IT, msg.subType);
    assertEquals("Sensor Id", "2061.1", msg.generateDeviceId());
    assertEquals("Command", ON, msg.command);
  }
Esempio n. 18
0
  public void printInfo(PrintStream ps) {
    AssetObjectPathTable objTable = asset.getObjectPaths();
    AssetRefTable refTable = asset.getReferences();
    AssetHeader header = asset.getHeader();
    AssetTypeTree fieldTree = asset.getTypeTree();

    ps.println("Header");
    ps.println("  File size: " + humanReadableByteCount(header.fileSize, true));
    ps.println("  Tree size: " + humanReadableByteCount(header.treeSize, true));
    ps.println("  Format: " + header.format);
    ps.println("  Data offset: " + header.dataOffset);
    ps.println("  Unknown: " + header.unknown);
    ps.println();

    ps.println("Serialized data");
    ps.println("  Revision: " + fieldTree.revision);
    ps.println("  Version: " + fieldTree.version);
    ps.println("  Standalone: " + (fieldTree.isStandalone() ? "yes" : "no"));
    ps.println("  Objects: " + objTable.size());
    ps.println();

    if (!refTable.isEmpty()) {
      ps.println("External references");
      for (AssetRef ref : refTable) {
        if (!ref.assetPath.isEmpty()) {
          ps.printf("  Asset path: \"%s\"\n", ref.assetPath);
        }
        if (!ref.filePath.isEmpty()) {
          ps.printf("  File path: \"%s\"\n", ref.filePath);
        }
        ps.printf("  GUID: %s\n", DatatypeConverter.printHexBinary(ref.guid));
        ps.printf("  Type: %d\n", ref.type);
        ps.println();
      }
    }
  }
Esempio n. 19
0
 public static String generateSalt() {
   byte[] saltBytes = new byte[SALT_SIZE];
   new SecureRandom().nextBytes(saltBytes);
   return DatatypeConverter.printHexBinary(saltBytes);
 }
 private String print(byte[] data) {
   return DatatypeConverter.printHexBinary(data)
       .toUpperCase()
       .replaceAll("(\\w{2})", "$1 ")
       .trim();
 }
Esempio n. 21
0
 /**
  * Calculates the MD5 hash of the given String, and returns it in its hexadecimal representation,
  * as lowercase characters.
  *
  * @param str String to MD5 hash.
  * @return Hexadecimal representation of the MD5 hash of the given String.
  */
 private static String md5(String str) {
   return DatatypeConverter.printHexBinary(
           MD5_DIGEST.digest(str.getBytes(StandardCharsets.ISO_8859_1)))
       .toLowerCase(Locale.ENGLISH);
 }
Esempio n. 22
0
 public static String toHexDump(InputStream in) throws IOException {
   return Joiner.on("\n")
       .join(
           Splitter.fixedLength(128)
               .split(DatatypeConverter.printHexBinary(ByteStreams.toByteArray(in))));
 }