Example usage for org.apache.shiro.util ByteSource getBytes

List of usage examples for org.apache.shiro.util ByteSource getBytes

Introduction

In this page you can find the example usage for org.apache.shiro.util ByteSource getBytes.

Prototype

byte[] getBytes();

Source Link

Document

Returns the wrapped byte array.

Usage

From source file:CryptoTest.java

License:Apache License

@Test
public void test_hashingService() {
    log.info("*** test_hashingService ***");
    final DefaultHashService hashService = new DefaultHashService();

    final SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
    secureRandomNumberGenerator.setDefaultNextBytesSize(64);
    final ByteSource privateSalt = secureRandomNumberGenerator.nextBytes();
    final ByteSource publicSalt = secureRandomNumberGenerator.nextBytes();

    log.info("privateSalt .length = {}", privateSalt.getBytes().length);

    hashService.setHashAlgorithmName("SHA-512");
    hashService.setHashIterations(1024 * 64);
    hashService.setPrivateSalt(privateSalt);
    hashService.setRandomNumberGenerator(secureRandomNumberGenerator);
    hashService.setGeneratePublicSalt(true);

    final HashRequest hashRequest = new HashRequest.Builder().setSource("password").setSalt(publicSalt).build();
    final Hash hash = hashService.computeHash(hashRequest);
    log.info("hash.salt : {}", hash.getSalt());
    log.info("publicSalt : {}", publicSalt);
    log.info("hash Base64 : {}", hash.toBase64());
    final String hash1 = hashService.computeHash(hashRequest).toBase64();
    final String hash2 = hashService.computeHash(hashRequest).toBase64();
    log.info("hash1 Base64 : {}", hash1);
    log.info("hash2 Base64 : {}", hash2);
    Assert.assertEquals(hash1, hash2);//from   w ww  . j  a  v a  2s.c  o  m

    Sha512Hash encodedPassword = new Sha512Hash("password", publicSalt, 1024 * 64);
    Sha512Hash encodedPassword2 = new Sha512Hash(encodedPassword.getBytes(), privateSalt, 1024 * 64);
    log.info("encodedPassword Base64 : {}", encodedPassword.toBase64());
    log.info("encodedPassword2 Base64 : {}", encodedPassword2.toBase64());

    Sha512Hash encodedPassword3 = new Sha512Hash("password", publicSalt, 1024 * 64);
    Sha512Hash encodedPassword4 = new Sha512Hash(encodedPassword3.getBytes(), privateSalt, 1024 * 64);
    log.info("encodedPassword3 Base64 : {}", encodedPassword3.toBase64());
    log.info("encodedPassword4 Base64 : {}", encodedPassword4.toBase64());

    Assert.assertEquals(encodedPassword2, encodedPassword4);
}

From source file:CryptoTest.java

License:Apache License

@Test
public void test_secureRandomNumberGenerator_nextBytesSize() {
    log.info("*** test_secureRandomNumberGenerator_nextBytesSize ***");
    final DefaultHashService hashService = new DefaultHashService();
    final SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator();
    secureRandomNumberGenerator.setDefaultNextBytesSize(8);
    final ByteSource privateSalt = secureRandomNumberGenerator.nextBytes();
    log.info("privateSalt = {}", privateSalt);
    log.info("privateSalt byte length = {}", privateSalt.getBytes().length);

    hashService.setHashAlgorithmName("SHA-512");
    hashService.setHashIterations(1024 * 128);
    hashService.setPrivateSalt(privateSalt);
    hashService.setRandomNumberGenerator(secureRandomNumberGenerator);
    hashService.setGeneratePublicSalt(true);

    final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build();
    final Hash hash = hashService.computeHash(hashRequest);

    final DefaultHashService hashService2 = new DefaultHashService();
    final SecureRandomNumberGenerator secureRandomNumberGenerator2 = new SecureRandomNumberGenerator();
    secureRandomNumberGenerator2.setDefaultNextBytesSize(16);

    hashService2.setHashAlgorithmName("SHA-512");
    hashService2.setHashIterations(1024 * 128);
    hashService2.setPrivateSalt(privateSalt);
    hashService2.setRandomNumberGenerator(secureRandomNumberGenerator2);
    hashService2.setGeneratePublicSalt(true);

    final HashRequest hashRequest2 = new HashRequest.Builder().setSource("password").setSalt(hash.getSalt())
            .build();/*  www.j av a2s . c  o  m*/
    final Hash hash2 = hashService.computeHash(hashRequest2);

    log.info("hash = {}", hash.toBase64());
    log.info("hash2 = {}", hash2.toBase64());

    Assert.assertEquals(hash2.toBase64(), hash.toBase64());
}

From source file:annis.security.SerializableByteSource.java

License:Apache License

public SerializableByteSource(ByteSource source) {
    if (source == null) {
        this.bytes = new byte[0];
    } else {// w w w  .  j a v a2s  . c o  m
        this.bytes = source.getBytes();
    }
}

From source file:br.com.criativasoft.opendevice.restapi.auth.AesRuntimeCipher.java

License:Open Source License

public String decript(String text) {
    ByteSource decrypt = cipher.decrypt(Base64.decode(text), key);
    return new String(decrypt.getBytes());
}

From source file:cn.powerdash.libsystem.common.security.util.CryptoUtil.java

License:Open Source License

public static String decrypt(String cipherText, String key) {
    AesCipherService aesCs = new AesCipherService();
    byte[] keyBytes = CodecSupport.toBytes(key);

    ByteSource decryptedBytes = aesCs.decrypt(Base64.decode(CodecSupport.toBytes(cipherText)), keyBytes);
    return CodecSupport.toString(decryptedBytes.getBytes());
}

From source file:com.ning.billing.server.security.KillbillJdbcRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(final AuthenticationToken token)
        throws AuthenticationException {
    final SimpleAuthenticationInfo authenticationInfo = (SimpleAuthenticationInfo) super.doGetAuthenticationInfo(
            token);//w  w w. j  a va2 s  .com

    // We store the salt bytes in Base64 (because the JdbcRealm retrieves it as a String)
    final ByteSource base64Salt = authenticationInfo.getCredentialsSalt();
    authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(Base64.decode(base64Salt.getBytes())));

    return authenticationInfo;
}

From source file:com.rencw.framework.shiro.SimpleByteSource.java

License:Apache License

/**
 * Creates an instance using the sources bytes directly - it does not create a copy of the
 * argument's byte array./*w  w w.j  a v a  2s .c o  m*/
 *
 * @param source the source to use to populate the underlying byte array.
 * @since 1.1
 */
public SimpleByteSource(ByteSource source) {
    this.bytes = source.getBytes();
}

From source file:com.rencw.framework.shiro.SimpleByteSource.java

License:Apache License

public boolean equals(Object o) {
    if (o == this) {
        return true;
    }/*from  ww  w .  j a  v  a 2s.  c  o m*/
    if (o instanceof ByteSource) {
        ByteSource bs = (ByteSource) o;
        return Arrays.equals(getBytes(), bs.getBytes());
    }
    return false;
}

From source file:com.sanweibook.lingdu.shiro.util.MySimpleByteSource.java

License:Apache License

/**
 * Creates an instance using the sources bytes directly - it does not create a copy of the
 * argument's byte array./*www  .java2  s .c om*/
 *
 * @param source the source to use to populate the underlying byte array.
 * @since 1.1
 */
public MySimpleByteSource(ByteSource source) {
    this.bytes = source.getBytes();
}

From source file:com.streamreduce.util.SecurityUtil.java

License:Apache License

/**
 * Returns decrypted password using given key
 *
 * @param password the password to decrypt in base64 encoding
 * @param key      the key used to encrypt the password
 * @return decrypted password in plain text
 *//*from   w ww.  j a  v a2  s .  c om*/
public static String decryptPassword(String password, byte[] key) {
    AesCipherService cipherService = new AesCipherService();
    ByteSource decrypted = cipherService.decrypt(Base64.decode(password), key);
    return new String(decrypted.getBytes());
}