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

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

Introduction

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

Prototype

String toHex();

Source Link

Document

Returns the <a href="http://en.wikipedia.org/wiki/Hexadecimal">Hex</a>-formatted String representation of the underlying wrapped byte array.

Usage

From source file:com.enioka.jqm.api.Dto2Jpa.java

License:Open Source License

private static RUser setJpa(EntityManager em, RUserDto dto) {
    RUser jpa = null;// w  w w  .j  a v a  2  s  .co m

    if (dto.getId() == null) {
        jpa = new RUser();
    } else {
        jpa = em.find(RUser.class, dto.getId());
    }

    jpa.setEmail(dto.getEmail());
    jpa.setExpirationDate(dto.getExpirationDate());
    jpa.setFreeText(dto.getFreeText());
    jpa.setLocked(dto.getLocked());
    jpa.setLogin(dto.getLogin());

    jpa = em.merge(jpa);

    RRole r = null;
    for (RRole ex : jpa.getRoles()) {
        ex.getUsers().remove(jpa);
        // jpa.getRoles().remove(ex);
    }
    for (Integer rid : dto.getRoles()) {
        r = em.find(RRole.class, rid);
        if (r == null) {
            throw new ErrorDto("Trying to associate an account with a non-existing role", "", 4,
                    Status.BAD_REQUEST);
        }
        jpa.getRoles().add(r);
        r.getUsers().add(jpa);
    }

    if (dto.getNewPassword() != null && !dto.getNewPassword().isEmpty()) {
        ByteSource salt = new SecureRandomNumberGenerator().nextBytes();
        jpa.setPassword(new Sha512Hash(dto.getNewPassword(), salt, 100000).toHex());
        jpa.setHashSalt(salt.toHex());
    }

    // Done
    return jpa;
}

From source file:com.enioka.jqm.api.SimpleApiSecurity.java

License:Open Source License

/**
 * Will create (or recreate) if necessary the temporary login data.<br>
 * Will create its own transaction - therefore the given em must not have any active transaction.
 *///  w  w  w.  ja  v a  2s . c  om
static Duet getId(EntityManager em) {
    if (logindata == null && useAuth == null) {
        try {
            GlobalParameter gp = em
                    .createQuery("SELECT gp from GlobalParameter gp WHERE gp.key = 'enableWsApiAuth'",
                            GlobalParameter.class)
                    .getSingleResult();
            useAuth = Boolean.parseBoolean(gp.getValue());
        } catch (NoResultException e) {
            useAuth = true;
        }

        if (!useAuth) {
            jqmlogger.debug("The client API will not use any authentication to download files");
            logindata = new Duet();
            logindata.pass = null;
            logindata.usr = null;
        } else {
            jqmlogger.debug("The client API will use authentication to download files");
        }
    }

    if (!useAuth) {
        return logindata;
    }

    if (user == null || user.getExpirationDate().before(Calendar.getInstance())) {
        synchronized (lock) {
            if (user == null || user.getExpirationDate().before(Calendar.getInstance())) {
                jqmlogger.debug(
                        "The client API will create an internal secret to access the simple API for file downloading");
                em.getTransaction().begin();

                // Create new
                user = new RUser();
                secret = UUID.randomUUID().toString();
                Calendar expiration = Calendar.getInstance();
                expiration.add(Calendar.DAY_OF_YEAR, 1);
                user.setExpirationDate(expiration);
                user.setInternal(true);
                user.setLocked(false);
                user.setLogin(UUID.randomUUID().toString());

                ByteSource salt = new SecureRandomNumberGenerator().nextBytes();
                user.setPassword(new Sha512Hash(secret, salt, 100000).toHex());
                user.setHashSalt(salt.toHex());
                em.persist(user);

                logindata = new Duet();
                logindata.pass = secret;
                logindata.usr = user.getLogin();

                RRole r = em.createQuery("SELECT r from RRole r where r.name = 'administrator'", RRole.class)
                        .getSingleResult();
                r.getUsers().add(user);

                // Purge all old internal accounts
                for (RUser ru : em
                        .createQuery("SELECT u FROM RUser u WHERE u.internal = true AND u.expirationDate < :n",
                                RUser.class)
                        .setParameter("n", Calendar.getInstance()).getResultList()) {
                    // Not using DELETE query but a remove in a loop because two-ways M2M relationship are stupid in JPA.
                    for (RRole rr : ru.getRoles()) {
                        rr.getUsers().remove(ru);
                    }
                    ru.getRoles().clear();
                    em.remove(ru);
                }

                em.getTransaction().commit();
            }
        }
    }

    return logindata;
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

License:Open Source License

public static void encodePassword(RUser user) {
    ByteSource salt = new SecureRandomNumberGenerator().nextBytes();
    user.setPassword(new Sha512Hash(user.getPassword(), salt, 100000).toHex());
    user.setHashSalt(salt.toHex());
}

From source file:com.enioka.jqm.tools.Helpers.java

License:Open Source License

static void encodePassword(RUser user) {
    ByteSource salt = new SecureRandomNumberGenerator().nextBytes();
    user.setPassword(new Sha512Hash(user.getPassword(), salt, 100000).toHex());
    user.setHashSalt(salt.toHex());
}

From source file:eu.eubrazilcc.lvl.storage.security.shiro.CryptProvider.java

License:EUPL

/**
 * Encodes a {@link ByteSource} to a hex-encoded string representation.
 * @param source - {@link ByteSource} to be encoded
 * @return a hex-encoded string representation of the input source.
 *//*from www . j av  a2s.  c  o m*/
private static String encodeHex(final ByteSource source) {
    return source.toHex();
}

From source file:net.fm.secwerk.auth.domain.model.Account.java

/**
 * //from  www  .  j  av  a2  s  .  c o  m
 * @param newPassword 
 */
public final void createNewPassword(String newPassword) {
    RandomNumberGenerator rng = new SecureRandomNumberGenerator();
    ByteSource _salt = rng.nextBytes();
    String hashedPassword = new Sha512Hash(newPassword, _salt, PasswordCredentialsMatcher.HASH_ITERATIONS)
            .toBase64();
    setPassword(hashedPassword);
    setSalt(_salt.toHex());
}

From source file:org.cherchgk.services.SecurityService.java

License:Apache License

/**
 * ?   ?./*from  w  w w. jav a 2 s . c om*/
 *
 * @param user     
 * @param password .
 */
public void setUserPassword(User user, String password) {
    RandomNumberGenerator rng = new SecureRandomNumberGenerator();
    ByteSource salt = rng.nextBytes();
    String passwordHash = new Sha512Hash(password, salt, hashIterations).toHex();
    user.setPassword(passwordHash);
    user.setPasswordSalt(salt.toHex());
}

From source file:org.obiba.mica.micaConfig.service.MicaConfigService.java

License:Open Source License

public String encrypt(String plain) {
    ByteSource encrypted = cipherService.encrypt(CodecSupport.toBytes(plain), getSecretKey());
    return encrypted.toHex();
}

From source file:org.obiba.opal.core.service.security.CryptoServiceImpl.java

License:Open Source License

@Override
public String encrypt(String plain) {
    ByteSource encrypted = cipherService.encrypt(CodecSupport.toBytes(plain), getSecretKey());
    return encrypted.toHex();
}

From source file:org.sisto.jeeplate.security.shiro.Salt.java

License:Open Source License

public static String byteSourceToString(ByteSource bs) {
    return (bs.toHex());
}