Example usage for org.apache.shiro.crypto.hash Sha256Hash Sha256Hash

List of usage examples for org.apache.shiro.crypto.hash Sha256Hash Sha256Hash

Introduction

In this page you can find the example usage for org.apache.shiro.crypto.hash Sha256Hash Sha256Hash.

Prototype

public Sha256Hash(Object source, Object salt) 

Source Link

Usage

From source file:com.fengjing.framework.shiro.BootstrapDataPopulator.java

License:Apache License

public void afterPropertiesSet() throws Exception {
    //because we're using an in-memory hsqldb for the sample app, a new one will be created each time the
    //app starts, so create the tables and insert the 2 sample users on bootstrap:

    JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
    //jdbcTemplate.execute(CREATE_TABLES);

    System.out.println(CREATE_TABLES);

    //password is 'admin' SHA hashed and base64 encoded:
    //The first argument to the hash constructor is the actual value to be hased.  The 2nd is the
    //salt.  In this simple demo scenario, the username and the password are the same, but to clarify the
    //distinction, you would see this in practice:
    //new Sha256Hash( <password>, <cryptographically strong randomly generated salt> (not the username!) )
    String query = "insert into users values ('admin', '" + new Sha256Hash("admin", "admin").toBase64() + "' )";
    jdbcTemplate.execute(query);//  w  w  w.  j a v  a2 s.c o  m
    log.debug("admin.");

    //password is 'user' SHA hashed and base64 encoded:
    query = "insert into users values ( 'user', '" + new Sha256Hash("user", "user").toBase64() + "' )";
    jdbcTemplate.execute(query);
    log.debug("user.");

    query = "insert into roles values ( 'Admin' )";
    jdbcTemplate.execute(query);
    log.debug(" Admin");

    query = "insert into roles values ( 'User' )";
    jdbcTemplate.execute(query);
    log.debug(" User");

    query = "insert into roles_permissions values ( 'Admin', 'user:view')";
    jdbcTemplate.execute(query);
    log.debug("Adminuser:view");

    query = "insert into roles_permissions values ( 'Admin', 'user:edit')";
    jdbcTemplate.execute(query);
    log.debug("Adminuser:edit");

    query = "insert into roles_permissions values ( 'User', 'user:view')";
    jdbcTemplate.execute(query);
    log.debug("Useruser:view");

    query = "insert into user_roles values ( 'admin', 'Admin' )";
    jdbcTemplate.execute(query);
    query = "insert into user_roles values ( 'admin', 'User' )";
    jdbcTemplate.execute(query);
    log.debug("adminAdmin User");

    query = "insert into user_roles values ( 'user', 'User' )";
    jdbcTemplate.execute(query);
    log.debug("user User");
}

From source file:com.panlingxiao.shiro.spring.BootstrapDataPopulator.java

License:Apache License

public void afterPropertiesSet() throws Exception {
    //because we're using an in-memory hsqldb for the sample app, a new one will be created each time the
    //app starts, so create the tables and insert the 2 sample users on bootstrap:

    JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
    jdbcTemplate.execute(CREATE_TABLES);

    //password is 'user1' SHA hashed and base64 encoded:
    //The first argument to the hash constructor is the actual value to be hased.  The 2nd is the
    //salt.  In this simple demo scenario, the username and the password are the same, but to clarify the
    //distinction, you would see this in practice:
    //new Sha256Hash( <password>, <cryptographically strong randomly generated salt> (not the username!) )
    String query = "insert into users values ('user1', '" + new Sha256Hash("user1", "user1").toBase64() + "' )";
    jdbcTemplate.execute(query);/*from w  w  w  .  ja v  a 2  s .c o m*/
    log.debug("Created user1.");

    //password is 'user2' SHA hashed and base64 encoded:
    query = "insert into users values ( 'user2', '" + new Sha256Hash("user2", "user2").toBase64() + "' )";
    jdbcTemplate.execute(query);
    log.debug("Created user2.");

    query = "insert into roles values ( 'role1' )";
    jdbcTemplate.execute(query);
    log.debug("Created role1");

    query = "insert into roles values ( 'role2' )";
    jdbcTemplate.execute(query);
    log.debug("Created role2");

    query = "insert into roles_permissions values ( 'role1', 'permission1')";
    jdbcTemplate.execute(query);
    log.debug("Created permission 1 for role 1");

    query = "insert into roles_permissions values ( 'role1', 'permission2')";
    jdbcTemplate.execute(query);
    log.debug("Created permission 2 for role 1");

    query = "insert into roles_permissions values ( 'role2', 'permission1')";
    jdbcTemplate.execute(query);
    log.debug("Created permission 1 for role 2");

    query = "insert into user_roles values ( 'user1', 'role1' )";
    jdbcTemplate.execute(query);
    query = "insert into user_roles values ( 'user1', 'role2' )";
    jdbcTemplate.execute(query);
    log.debug("Assigned user1 roles role1 and role2");

    query = "insert into user_roles values ( 'user2', 'role2' )";
    jdbcTemplate.execute(query);
    log.debug("Assigned user2 role role2");
}

From source file:com.shiro.app.util.HashGenerator.java

License:Apache License

/**
 * Salt hash password./*from   w  ww . j  av a2 s  .co m*/
 *
 * @param password the password
 * @param salt the salt
 * @return the string
 */
public String saltHashPassword(String password, String salt) {
    Sha256Hash hash = new Sha256Hash(password, (new SimpleByteSource(salt)).getBytes());
    return hash.toHex();
}

From source file:com.wegas.core.security.jparealm.JpaAccount.java

License:MIT License

/**
 *
 *//*from w w  w .j  av a  2  s .  com*/
@PreUpdate
public void preUpdate() {
    if (this.password != null && !this.password.isEmpty()) {
        this.passwordHex = new Sha256Hash(this.password, (new SimpleByteSource(this.getSalt())).getBytes())
                .toHex();
    }
}

From source file:edu.utpl.gestionTesis.seguridad.usuario.LoginController.java

public void actualizarClave() {
    List<PftPersona> personas = personaService.buscar(new PftPersona());
    for (PftPersona persona : personas) {
        Sha256Hash sha256Hash = new Sha256Hash(persona.getPerClave(),
                (new SimpleByteSource("random_salt_value_" + persona.getPerClave())).getBytes());
        String result = sha256Hash.toHex();
        persona.setPerClave(result);/*from   ww w  .j av a 2s .  c  o  m*/
        personaService.editar(persona);
    }
}

From source file:edu.utpl.gestionTesis.seguridad.usuario.LoginController.java

public String authenticate() {
    try {/* ww w  .ja va 2  s.c o m*/
        Sha256Hash sha256Hash = new Sha256Hash(sessionLogin.getPassword(),
                (new SimpleByteSource("random_salt_value_" + sessionLogin.getPassword())).getBytes());
        String result = sha256Hash.toHex();
        sessionLogin.setPassword(result);
        UsernamePasswordToken token = new UsernamePasswordToken(sessionLogin.getUsername(),
                sessionLogin.getPassword());
        //            token.setRememberMe(sessionLogin.getRemember());
        org.apache.shiro.subject.Subject currentUser = SecurityUtils.getSubject();
        currentUser.login(token);
        PftPersona personaBuscar = new PftPersona();
        personaBuscar.setPerUsuario(sessionLogin.getUsername());
        sessionLogin.setPersona(personaService.buscarPorUsuario(personaBuscar));
    } catch (Exception e) {
        cabeceraController.getMessageView().message(FacesMessage.SEVERITY_ERROR, "USUARIO INCORRECTO",
                e.getMessage());
        return "";
    }
    return "pretty:inicio";
}

From source file:edu.utpl.gestionTesis.seguridad.usuario.LoginController.java

public String cambiarClave() {
    try {//from ww  w  . j  av  a 2 s.c o  m
        if (sessionLogin.getConfirmarClave().equalsIgnoreCase(sessionLogin.getPersona().getPerClave())) {
            Sha256Hash sha256Hash = new Sha256Hash(sessionLogin.getPersona().getPerClave(),
                    (new SimpleByteSource("random_salt_value_" + sessionLogin.getPersona().getPerClave()))
                            .getBytes());
            String result = sha256Hash.toHex();
            sessionLogin.getPersona().setPerClave(result);
            personaService.editar(sessionLogin.getPersona());
            cabeceraController.getMessageView().message(FacesMessage.SEVERITY_INFO, "Clave Actualizada", "");
            return "pretty:inicio";
        } else {
            cabeceraController.getMessageView().message(FacesMessage.SEVERITY_ERROR, "Clave no coinciden", "");
        }
    } catch (Exception e) {
        System.out.println(e);
    }

    return "";
}

From source file:net.noday.core.utils.Digests.java

License:Apache License

public static String Sha256Hash(Object source) {
    return new Sha256Hash(source, HASH_INTERATIONS).toBase64();
}

From source file:zi.helper.ZHelper.java

License:Apache License

public static String simpleSaltedHash(String key1, String key2, String salt) {
    Sha256Hash sha256Hash = new Sha256Hash(key2, (new SimpleByteSource(salt + key1)).getBytes());
    String result = sha256Hash.toHex();

    ZHelper.logInfo(ZHelper.class, key1 + " simple salted hash: " + result);
    return result;
}