org.apache.shiro.codec.Hex类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(175)

本文整理了Java中org.apache.shiro.codec.Hex类的一些代码示例,展示了Hex类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Hex类的具体详情如下:
包路径:org.apache.shiro.codec.Hex
类名称:Hex

Hex介绍

[英][Hexadecimal](http://en.wikipedia.org/wiki/Hexadecimal) encoder and decoder.

This class was borrowed from Apache Commons Codec SVN repository (rev. 560660) with modifications to enable Hex conversion without a full dependency on Commons Codec. We didn't want to reinvent the wheel of great work they've done, but also didn't want to force every Shiro user to depend on the commons-codec.jar

As per the Apache 2.0 license, the original copyright notice and all author and copyright information have remained in tact.
[中][Hexadecimal](http://en.wikipedia.org/wiki/Hexadecimal)编码器和解码器。
这个类是从ApacheCommons编解码器SVN存储库(版本560660)中借用的,经过修改,可以在不完全依赖于Commons编解码器的情况下实现十六进制转换。我们不想重新发明他们所做的伟大工作,但也不想强迫每个Shiro用户依赖commons编解码器。罐子
根据Apache2.0许可,原始版权声明以及所有作者和版权信息保持不变。

代码示例

代码示例来源:origin: apache/shiro

/**
 * Converts the specified Hex-encoded String into a raw byte array.  This is a
 * convenience method that merely delegates to {@link #decode(char[])} using the
 * argument's hex.toCharArray() value.
 *
 * @param hex a Hex-encoded String.
 * @return A byte array containing binary data decoded from the supplied String's char array.
 */
public static byte[] decode(String hex) {
  return decode(hex.toCharArray());
}

代码示例来源:origin: Graylog2/graylog2-server

@Nullable
public static String encrypt(String plainText, String encryptionKey, String salt) {
  try {
    @SuppressWarnings("CIPHER_INTEGRITY")
    Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
    return Hex.encodeToString(cipher.doFinal(plainText.getBytes("UTF-8")));
  } catch (Exception e) {
    LOG.error("Could not encrypt value.", e);
  }
  return null;
}

代码示例来源:origin: apache/shiro

/**
 * Encodes the specified byte array to a character array and then returns that character array
 * as a String.
 *
 * @param bytes the byte array to Hex-encode.
 * @return A String representation of the resultant hex-encoded char array.
 */
public static String encodeToString(byte[] bytes) {
  char[] encodedChars = encode(bytes);
  return new String(encodedChars);
}

代码示例来源:origin: org.codeartisans.shiro/shiro-ext-x509-web

private String readHexSerialNumberFromString( String input )
{
  return Hex.encodeToString( Hex.decode( input ) ); // FIXME Upstream Hex implementation is not char encoding safe, fix it there
}

代码示例来源:origin: apache/shiro

/**
 * Converts an array of characters representing hexadecimal values into an
 * array of bytes of those same values. The returned array will be half the
 * length of the passed array, as it takes two characters to represent any
 * given byte. An exception is thrown if the passed char array has an odd
 * number of elements.
 *
 * @param data An array of characters containing hexadecimal digits
 * @return A byte array containing binary data decoded from
 *         the supplied char array.
 * @throws IllegalArgumentException if an odd number or illegal of characters
 *                                  is supplied
 */
public static byte[] decode(char[] data) throws IllegalArgumentException {
  int len = data.length;
  if ((len & 0x01) != 0) {
    throw new IllegalArgumentException("Odd number of characters.");
  }
  byte[] out = new byte[len >> 1];
  // two characters form the hex value.
  for (int i = 0, j = 0; j < len; i++) {
    int f = toDigit(data[j], j) << 4;
    j++;
    f = f | toDigit(data[j], j);
    j++;
    out[i] = (byte) (f & 0xFF);
  }
  return out;
}

代码示例来源:origin: SomMeri/matasano-cryptopals-solutions

public String xorHex(String first, String second) {
 return Hex.encodeToString(xor(Hex.decode(first), Hex.decode(second)));
}

代码示例来源:origin: org.apache.shiro/shiro-lang

/**
 * Converts an array of characters representing hexadecimal values into an
 * array of bytes of those same values. The returned array will be half the
 * length of the passed array, as it takes two characters to represent any
 * given byte. An exception is thrown if the passed char array has an odd
 * number of elements.
 *
 * @param data An array of characters containing hexadecimal digits
 * @return A byte array containing binary data decoded from
 *         the supplied char array.
 * @throws IllegalArgumentException if an odd number or illegal of characters
 *                                  is supplied
 */
public static byte[] decode(char[] data) throws IllegalArgumentException {
  int len = data.length;
  if ((len & 0x01) != 0) {
    throw new IllegalArgumentException("Odd number of characters.");
  }
  byte[] out = new byte[len >> 1];
  // two characters form the hex value.
  for (int i = 0, j = 0; j < len; i++) {
    int f = toDigit(data[j], j) << 4;
    j++;
    f = f | toDigit(data[j], j);
    j++;
    out[i] = (byte) (f & 0xFF);
  }
  return out;
}

代码示例来源:origin: Graylog2/graylog2-server

@Nullable
  public static String decrypt(String cipherText, String encryptionKey, String salt) {
    try {
      @SuppressWarnings("CIPHER_INTEGRITY")
      Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
      SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
      cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
      return new String(cipher.doFinal(Hex.decode(cipherText)), "UTF-8");
    } catch (Exception e) {
      LOG.error("Could not decrypt value.", e);
    }
    return null;
  }
}

代码示例来源:origin: apache/shiro

public String toHex() {
  if ( this.cachedHex == null ) {
    this.cachedHex = Hex.encodeToString(getBytes());
  }
  return this.cachedHex;
}

代码示例来源:origin: org.apache.shiro/shiro-lang

/**
 * Encodes the specified byte array to a character array and then returns that character array
 * as a String.
 *
 * @param bytes the byte array to Hex-encode.
 * @return A String representation of the resultant hex-encoded char array.
 */
public static String encodeToString(byte[] bytes) {
  char[] encodedChars = encode(bytes);
  return new String(encodedChars);
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.shiro

/**
 * Converts an array of characters representing hexidecimal values into an
 * array of bytes of those same values. The returned array will be half the
 * length of the passed array, as it takes two characters to represent any
 * given byte. An exception is thrown if the passed char array has an odd
 * number of elements.
 *
 * @param data An array of characters containing hexidecimal digits
 * @return A byte array containing binary data decoded from
 *         the supplied char array.
 * @throws IllegalArgumentException if an odd number or illegal of characters
 *                                  is supplied
 */
public static byte[] decode(char[] data) throws IllegalArgumentException {
  int len = data.length;
  if ((len & 0x01) != 0) {
    throw new IllegalArgumentException("Odd number of characters.");
  }
  byte[] out = new byte[len >> 1];
  // two characters form the hex value.
  for (int i = 0, j = 0; j < len; i++) {
    int f = toDigit(data[j], j) << 4;
    j++;
    f = f | toDigit(data[j], j);
    j++;
    out[i] = (byte) (f & 0xFF);
  }
  return out;
}

代码示例来源:origin: apache/shiro

protected byte[] toBytes(String sValue) {
  if (sValue == null) {
    return null;
  }
  byte[] bytes;
  if (sValue.startsWith(HEX_BEGIN_TOKEN)) {
    String hex = sValue.substring(HEX_BEGIN_TOKEN.length());
    bytes = Hex.decode(hex);
  } else {
    //assume base64 encoded:
    bytes = Base64.decode(sValue);
  }
  return bytes;
}

代码示例来源:origin: apache/shiro

/**
 * Returns a hex-encoded string of the underlying {@link #getBytes byte array}.
 * <p/>
 * This implementation caches the resulting hex string so multiple calls to this method remain efficient.
 * However, calling {@link #setBytes setBytes} will null the cached value, forcing it to be recalculated the
 * next time this method is called.
 *
 * @return a hex-encoded string of the underlying {@link #getBytes byte array}.
 */
public String toHex() {
  if (this.hexEncoded == null) {
    this.hexEncoded = Hex.encodeToString(getBytes());
  }
  return this.hexEncoded;
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.shiro

/**
 * Encodes the specifed byte array to a character array and then returns that character array
 * as a String.
 *
 * @param bytes the byte array to Hex-encode.
 * @return A String representation of the resultant hex-encoded char array.
 */
public static String encodeToString(byte[] bytes) {
  char[] encodedChars = encode(bytes);
  return new String(encodedChars);
}

代码示例来源:origin: apache/shiro

/**
 * Converts an array of character bytes representing hexadecimal values into an
 * array of bytes of those same values. The returned array will be half the
 * length of the passed array, as it takes two characters to represent any
 * given byte. An exception is thrown if the passed char array has an odd
 * number of elements.
 *
 * @param array An array of character bytes containing hexadecimal digits
 * @return A byte array containing binary data decoded from
 *         the supplied byte array (representing characters).
 * @throws IllegalArgumentException Thrown if an odd number of characters is supplied
 *                                  to this function
 * @see #decode(char[])
 */
public static byte[] decode(byte[] array) throws IllegalArgumentException {
  String s = CodecSupport.toString(array);
  return decode(s);
}

代码示例来源:origin: apache/shiro

/**
 * Returns a hex-encoded string of the underlying {@link #getBytes byte array}.
 * <p/>
 * This implementation caches the resulting hex string so multiple calls to this method remain efficient.
 * However, calling {@link #setBytes setBytes} will null the cached value, forcing it to be recalculated the
 * next time this method is called.
 *
 * @return a hex-encoded string of the underlying {@link #getBytes byte array}.
 */
public String toHex() {
  if (this.hexEncoded == null) {
    this.hexEncoded = Hex.encodeToString(getBytes());
  }
  return this.hexEncoded;
}

代码示例来源:origin: org.sonatype.nexus/nexus-ldap-common

protected String encodeString(String input) {
 InputStream is = new ByteArrayInputStream(input.getBytes());
 String result = null;
 try {
  byte[] buffer = new byte[1024];
  MessageDigest md5 = MessageDigest.getInstance("MD5");
  int numRead;
  do {
   numRead = is.read(buffer);
   if (numRead > 0) {
    md5.update(buffer, 0, numRead);
   }
  }
  while (numRead != -1);
  result = new String(Hex.encode(md5.digest()));
 }
 catch (Exception e) {
 }
 return result;
}

代码示例来源:origin: apache/shiro

public static Md5Hash fromHexString(String hex) {
  Md5Hash hash = new Md5Hash();
  hash.setBytes(Hex.decode(hex));
  return hash;
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
public void setSystemPassword(String systemPassword) {
  if (systemPassword == null || systemPassword.isEmpty()) {
    return;
  }
  // set new salt value, if we didn't have any.
  if (getSystemPasswordSalt().isEmpty()) {
    LOG.debug("Generating new salt for LDAP system password.");
    final SecureRandom random = new SecureRandom();
    byte[] saltBytes = new byte[8];
    random.nextBytes(saltBytes);
    setSystemPasswordSalt(Hex.encodeToString(saltBytes));
  }
  final String encrypted = AESTools.encrypt(
      systemPassword,
      configuration.getPasswordSecret().substring(0, 16),
      getSystemPasswordSalt());
  fields.put(SYSTEM_PASSWORD, encrypted);
}

代码示例来源:origin: SomMeri/matasano-cryptopals-solutions

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 String filename = (String) request.getParameter(FILE);
 String signature = (String) request.getParameter(SIGNATURE);
 
 boolean isValid = validate(filename, signature);
 
 response.setContentType("text/html");
 response.setStatus(HttpServletResponse.SC_OK);
 
 response.getWriter().println("<h1>Hello SimpleServlet</h1>");
 response.getWriter().println("filename: "+ filename + "<br>");
 response.getWriter().println("signature: "+ signature + "<br>");
 response.getWriter().println("expected: "+ new String(Hex.encode(expectedSignature(filename))) + "<br>");
 response.getWriter().println("<br>");
 response.getWriter().println("validity: "+ isValid + "<br>");
}

相关文章