org.apache.shiro.codec.Hex Java Examples

The following examples show how to use org.apache.shiro.codec.Hex. 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: MySimpleHash.java    From cms with Apache License 2.0 6 votes vote down vote up
protected byte[] hash(byte[] bytes, byte[] salt, int hashIterations) throws UnknownAlgorithmException {
    String toB = new String(bytes);
    String toS = new String(salt);
    String name = this.getAlgorithmName();
    MessageDigest digest = this.getDigest(this.getAlgorithmName());
    if (salt != null) {
        digest.reset();
        digest.update(salt);
    }

    byte[] hashed = digest.digest(bytes);
    String toH = Hex.encodeToString(hashed);
    int iterations = hashIterations - 1;

    for(int i = 0; i < iterations; ++i) {
        digest.reset();
        hashed = digest.digest(hashed);
    }
    toH = Hex.encodeToString(hashed);
    return hashed;
}
 
Example #2
Source File: SHA512.java    From JavaSecurity with Apache License 2.0 6 votes vote down vote up
private static boolean verifyPassword(byte[] originalHash, ByteSource publicSalt, String password) {
    ByteSource privateSalt = ByteSource.Util.bytes(PRIVATE_SALT_BYTES);
    DefaultHashService hashService = new DefaultHashService();
    hashService.setPrivateSalt(privateSalt);
    hashService.setHashIterations(ITERATIONS);

    HashRequest.Builder builder = new HashRequest.Builder();
    builder.setSource(ByteSource.Util.bytes(password));
    builder.setSalt(publicSalt);

    Hash comparisonHash = hashService.computeHash(builder.build());

    log.info("password: {}", password);
    log.info("1 hash: {}", Hex.encodeToString(originalHash));
    log.info("2 hash: {}", comparisonHash.toHex());

    return Arrays.equals(originalHash, comparisonHash.getBytes());
}
 
Example #3
Source File: HashedCredentialsMatcher.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param info the AuthenticationInfo from which to retrieve the credentials which assumed to be in already-hashed form.
 * @return a {@link Hash Hash} instance representing the given AuthenticationInfo's stored credentials.
 */
protected Object getCredentials(AuthenticationInfo info) {
    Object credentials = info.getCredentials();

    byte[] storedBytes = toBytes(credentials);

    if (credentials instanceof String || credentials instanceof char[]) {
        //account.credentials were a char[] or String, so
        //we need to do text decoding first:
        if (isStoredCredentialsHexEncoded()) {
            storedBytes = Hex.decode(storedBytes);
        } else {
            storedBytes = Base64.decode(storedBytes);
        }
    }
    AbstractHash hash = newHashInstance();
    hash.setBytes(storedBytes);
    return hash;
}
 
Example #4
Source File: WebPageSource.java    From cms with Apache License 2.0 6 votes vote down vote up
@Test
    public void hsah3() throws NoSuchAlgorithmException {
        byte[] salt = "123456".getBytes();
        int hashIterations = 2;
        MessageDigest digest = MessageDigest.getInstance("MD5");
        if (salt != null) {
            digest.reset();
            digest.update(salt);
        }

        byte[] hashed = digest.digest();
//        int iterations = hashIterations - 1;
//
//        for (int i = 0; i < iterations; ++i) {
//            digest.reset();
//            hashed = digest.digest(hashed);
//        }

        System.out.println(Hex.encodeToString(hashed));
    }
 
Example #5
Source File: WebPageSource.java    From cms with Apache License 2.0 6 votes vote down vote up
@Test
    public void hsah() throws NoSuchAlgorithmException {
        byte[] salt = "demo5909af55d288d8f2581f7d572f2eb6bb".getBytes(); //Base64.encodeToString("123456".getBytes()).getBytes();
        byte[] bytes = "123456".getBytes(); //Base64.encodeToString("demo5909af55d288d8f2581f7d572f2eb6bb".getBytes()).getBytes();
        int hashIterations = 2;
        MessageDigest digest = MessageDigest.getInstance("MD5");
        if (salt != null) {
            digest.reset();
            digest.update(salt);
        }

        byte[] hashed = digest.digest(bytes);
        int iterations = hashIterations - 1;

//        for (int i = 0; i < iterations; ++i) {
//            digest.reset();
//            hashed = digest.digest(hashed);
//        }

        System.out.println(Hex.encodeToString(hashed));
    }
 
Example #6
Source File: EndecryptUtil.java    From LazyREST with Apache License 2.0 5 votes vote down vote up
/**
 * AES解密
 *
 * @param content
 * @return
 */

public static String decryptAES(String content) {
    AesCipherService aesCipherService = new AesCipherService();
    aesCipherService.setKeySize(128);
    return new String(aesCipherService.decrypt(Hex.decode(content), key.getEncoded()).getBytes());
}
 
Example #7
Source File: WebPageSource.java    From cms with Apache License 2.0 5 votes vote down vote up
@Test
public void hsah2() throws NoSuchAlgorithmException {
    byte[] bytes = "demo5909af55d288d8f2581f7d572f2eb6bb123456".getBytes();
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.update(bytes);
    byte[] hashed = digest.digest();

    System.out.println(Hex.encodeToString(hashed));
}
 
Example #8
Source File: MySimpleHash.java    From cms with Apache License 2.0 5 votes vote down vote up
public String toHex() {
    if (this.hexEncoded == null) {
        this.hexEncoded = Hex.encodeToString(this.getBytes());
    }

    return this.hexEncoded;
}
 
Example #9
Source File: AbstractHash.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @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;
}
 
Example #10
Source File: SimpleHash.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @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;
}
 
Example #11
Source File: ShiroByteSource.java    From xmanager with Apache License 2.0 5 votes vote down vote up
@Override
public String toHex() {
	if ( this.cachedHex == null ) {
		this.cachedHex = Hex.encodeToString(getBytes());
	}
	return this.cachedHex;
}
 
Example #12
Source File: MySimpleHash.java    From cms with Apache License 2.0 4 votes vote down vote up
private void hash(ByteSource source, ByteSource salt, int hashIterations) throws CodecException, UnknownAlgorithmException {
    byte[] saltBytes = salt != null ? salt.getBytes() : null;
    byte[] hashedBytes = this.hash(source.getBytes(), saltBytes, hashIterations);
    String toH= Hex.encodeToString(hashedBytes);
    this.setBytes(hashedBytes);
}
 
Example #13
Source File: DigestUtil.java    From UserCenter with MIT License 4 votes vote down vote up
/**
 * 解密
 */
public static String Decrypt(String value) {
    log.info("value: " + value);
    return new String(aesCipherService.decrypt(
            Hex.decode(value), bytes).getBytes());
}
 
Example #14
Source File: MySimpleByteSource.java    From VideoMeeting with Apache License 2.0 4 votes vote down vote up
public String toHex() {
	if (this.cachedHex == null) {
		this.cachedHex = Hex.encodeToString(getBytes());
	}
	return this.cachedHex;
}
 
Example #15
Source File: Md2Hash.java    From nano-framework with Apache License 2.0 4 votes vote down vote up
public static Md2Hash fromHexString(String hex) {
    Md2Hash hash = new Md2Hash();
    hash.setBytes(Hex.decode(hex));
    return hash;
}
 
Example #16
Source File: SimpleByteSource.java    From nano-framework with Apache License 2.0 4 votes vote down vote up
public String toHex() {
    if ( this.cachedHex == null ) {
        this.cachedHex = Hex.encodeToString(getBytes());
    }
    return this.cachedHex;
}
 
Example #17
Source File: AES.java    From JavaSecurity with Apache License 2.0 4 votes vote down vote up
private static void printReadableMessages(String initialText, byte[] ciphertext, byte[] plaintext) {
    log.info("initialText: {}", initialText);
    log.info("cipherText as HEX: {}", Hex.encodeToString(ciphertext));
    log.info("plaintext: {}", CodecSupport.toString(plaintext));
}
 
Example #18
Source File: EndecryptUtil.java    From LazyREST with Apache License 2.0 2 votes vote down vote up
/**
 * 16进制解密
 *
 * @param content
 * @return
 */
public static String decryptHex(String content) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(content), "消息摘要不能为空");
    return new String(Hex.decode(content));
}
 
Example #19
Source File: EndecryptUtil.java    From LazyREST with Apache License 2.0 2 votes vote down vote up
/**
 * 16进制加密
 *
 * @param content
 * @return
 */
public static String encrytHex(String content) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(content), "不能为空");
    byte[] bytes = content.getBytes();
    return Hex.encodeToString(bytes);
}