Java Code Examples for org.apache.commons.codec.digest.DigestUtils#sha1()

The following examples show how to use org.apache.commons.codec.digest.DigestUtils#sha1() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeakMessageDigest.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void apacheApiVariations() {

        printHex(DigestUtils.getMd2Digest().digest("123".getBytes()));
        printHex(DigestUtils.getMd5Digest().digest("123".getBytes()));
        printHex(DigestUtils.getDigest("md2").digest("123".getBytes()));
        printHex(DigestUtils.getDigest("md5").digest("123".getBytes()));
        DigestUtils.md2("123".getBytes());
        DigestUtils.md5("123".getBytes());
        System.out.println(DigestUtils.md2Hex("123".getBytes()));
        System.out.println(DigestUtils.md5Hex("123".getBytes()));

        printHex(DigestUtils.getSha1Digest().digest("123".getBytes()));
        printHex(DigestUtils.getShaDigest().digest("123".getBytes()));
        printHex(DigestUtils.getDigest("sha1").digest("123".getBytes()));
        printHex(DigestUtils.getDigest("sha-1").digest("123".getBytes()));
        DigestUtils.sha1("123".getBytes());
        DigestUtils.sha("123".getBytes());
        DigestUtils.sha1Hex("123".getBytes());
        DigestUtils.shaHex("123".getBytes());

        printHex(DigestUtils.getDigest("sha256").digest("123".getBytes())); //OK!
        printHex(DigestUtils.getDigest(getDigest()).digest("123".getBytes())); //Unknown
    }
 
Example 2
Source File: NativeLibrary.java    From The-5zig-Mod with GNU General Public License v3.0 5 votes vote down vote up
private static String loadLibrary(String fileName) throws IOException {
	InputStream resourceAsStream = null;
	byte[] resourceSha1;
	try {
		resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("core/native/" + fileName);
		resourceSha1 = DigestUtils.sha1(resourceAsStream);
	} finally {
		IOUtils.closeQuietly(resourceAsStream);
	}
	File dir = new File(The5zigMod.getModDirectory(), "native");
	if (!dir.exists() && !dir.mkdirs()) {
		throw new IOException("Could not create native directory!");
	}
	File file = new File(dir, fileName);
	InputStream inputStream = null;
	try {
		if (!file.exists() || !Arrays.equals(resourceSha1, DigestUtils.sha1(inputStream = new FileInputStream(file)))) {
			IOUtils.closeQuietly(inputStream);
			resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("core/native/" + fileName);
			FileUtils.copyInputStreamToFile(resourceAsStream, file);
		}
	} finally {
		IOUtils.closeQuietly(inputStream);
		IOUtils.closeQuietly(resourceAsStream);
	}
	String absolutePath = file.getAbsolutePath();
	System.load(absolutePath);

	return absolutePath;
}
 
Example 3
Source File: NativeLibrary.java    From The-5zig-Mod with MIT License 5 votes vote down vote up
private static String loadLibrary(String fileName) throws IOException {
	InputStream resourceAsStream = null;
	byte[] resourceSha1;
	try {
		resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("core/native/" + fileName);
		resourceSha1 = DigestUtils.sha1(resourceAsStream);
	} finally {
		IOUtils.closeQuietly(resourceAsStream);
	}
	File dir = new File(The5zigMod.getModDirectory(), "native");
	if (!dir.exists() && !dir.mkdirs()) {
		throw new IOException("Could not create native directory!");
	}
	File file = new File(dir, fileName);
	InputStream inputStream = null;
	try {
		if (!file.exists() || !Arrays.equals(resourceSha1, DigestUtils.sha1(inputStream = new FileInputStream(file)))) {
			IOUtils.closeQuietly(inputStream);
			resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("core/native/" + fileName);
			FileUtils.copyInputStreamToFile(resourceAsStream, file);
		}
	} finally {
		IOUtils.closeQuietly(inputStream);
		IOUtils.closeQuietly(resourceAsStream);
	}
	String absolutePath = file.getAbsolutePath();
	System.load(absolutePath);

	return absolutePath;
}
 
Example 4
Source File: MySQLAuthenticationHandler.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private byte[] getAuthCipherBytes(final String password) {
    byte[] sha1Password = DigestUtils.sha1(password);
    byte[] doubleSha1Password = DigestUtils.sha1(sha1Password);
    byte[] concatBytes = new byte[authPluginData.getAuthPluginData().length + doubleSha1Password.length];
    System.arraycopy(authPluginData.getAuthPluginData(), 0, concatBytes, 0, authPluginData.getAuthPluginData().length);
    System.arraycopy(doubleSha1Password, 0, concatBytes, authPluginData.getAuthPluginData().length, doubleSha1Password.length);
    byte[] sha1ConcatBytes = DigestUtils.sha1(concatBytes);
    return xor(sha1Password, sha1ConcatBytes);
}
 
Example 5
Source File: PasswordManager.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
private boolean verifySHA1Password(String storedCredential, String passedPassword) {
    //Remove the SHA_PREFIX from the password string
    String storedPassword = storedCredential.substring(SHA_PREFIX.length());

    //Get the SHA digest and encode it in Base64
    byte[] digestedPasswordBytes = DigestUtils.sha1(passedPassword);
    String digestedPassword = Base64.getEncoder().encodeToString(digestedPasswordBytes);

    //Check if the stored password matches the passed one
    return digestedPassword.equals(storedPassword);
}
 
Example 6
Source File: Uuid5Evaluator.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public QueryResult<String> evaluate(final EvaluationContext evaluationContext) {
  final String subjectValue = subject.evaluate(evaluationContext).getValue();
  if (subjectValue == null) {
      return new StringQueryResult(null);
  }
  final String nsValue = namespace.evaluate(evaluationContext).getValue();
  final UUID nsUUID = nsValue == null ? new UUID(0, 0) : UUID.fromString(nsValue);

  final byte[] nsBytes =
      ByteBuffer.wrap(new byte[16])
          .putLong(nsUUID.getMostSignificantBits())
          .putLong(nsUUID.getLeastSignificantBits())
          .array();

  final byte[] subjectBytes = subjectValue.getBytes();
  final byte[] nameBytes = ArrayUtils.addAll(nsBytes, subjectBytes);
  final byte[] nameHash = DigestUtils.sha1(nameBytes);
  final byte[] uuidBytes = Arrays.copyOf(nameHash, 16);

  uuidBytes[6] &= 0x0F;
  uuidBytes[6] |= 0x50;
  uuidBytes[8] &= 0x3f;
  uuidBytes[8] |= 0x80;

  return new StringQueryResult(toString(uuidBytes));
}
 
Example 7
Source File: FileUtil.java    From gocd with Apache License 2.0 5 votes vote down vote up
public static String sha1Digest(File file) {
    try(InputStream is = new BufferedInputStream(new FileInputStream(file))){
        byte[] hash = DigestUtils.sha1(is);
        return Base64.getEncoder().encodeToString(hash);
    } catch (IOException e) {
        throw ExceptionUtils.bomb(e);
    }
}
 
Example 8
Source File: HashCrypto.java    From wind-im with Apache License 2.0 4 votes vote down vote up
public static String SHA1(String key) {
	byte[] data = DigestUtils.sha1(key);
	return Hex.encodeHexString(data);
}
 
Example 9
Source File: HashCrypto.java    From wind-im with Apache License 2.0 4 votes vote down vote up
public static byte[] SHA1Bytes(String key) {
	return DigestUtils.sha1(key);
}
 
Example 10
Source File: HashCrypto.java    From openzaly with Apache License 2.0 4 votes vote down vote up
public static String SHA1(String key) {
	byte[] data = DigestUtils.sha1(key);
	return Hex.encodeHexString(data);
}
 
Example 11
Source File: HashCrypto.java    From openzaly with Apache License 2.0 4 votes vote down vote up
public static byte[] SHA1Bytes(String key) {
	return DigestUtils.sha1(key);
}
 
Example 12
Source File: HashCrypto.java    From openzaly with Apache License 2.0 4 votes vote down vote up
public static String SHA1(String key) {
	byte[] data = DigestUtils.sha1(key);
	return Hex.encodeHexString(data);
}
 
Example 13
Source File: HashCrypto.java    From openzaly with Apache License 2.0 4 votes vote down vote up
public static byte[] SHA1Bytes(String key) {
	return DigestUtils.sha1(key);
}
 
Example 14
Source File: CodecUtils.java    From juice with Apache License 2.0 4 votes vote down vote up
/** SHA-1 **/
public static byte[] sha1(final String data) {
    return DigestUtils.sha1(data);
}
 
Example 15
Source File: CodecUtils.java    From juice with Apache License 2.0 4 votes vote down vote up
public static byte[] sha1(final byte[] data) {
    return DigestUtils.sha1(data);
}
 
Example 16
Source File: CodecUtils.java    From juice with Apache License 2.0 4 votes vote down vote up
public static byte[] sha1(final InputStream in) throws IOException {
    return DigestUtils.sha1(in);
}