Example usage for org.apache.commons.codec.digest DigestUtils sha256Hex

List of usage examples for org.apache.commons.codec.digest DigestUtils sha256Hex

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest DigestUtils sha256Hex.

Prototype

public static String sha256Hex(String data) 

Source Link

Usage

From source file:com.squid.kraken.v4.core.analysis.engine.processor.AnalysisSmartCacheRequest.java

public String computeFiltersSignature(Universe universe, List<Axis> filters) {
    // sort the domains
    StringBuilder signature = new StringBuilder();
    Collections.sort(filters, new Comparator<Axis>() {
        @Override/*from  ww  w .  j  a  v  a2  s . c o  m*/
        public int compare(Axis o1, Axis o2) {
            return o1.getId().compareTo(o2.getId());
        }
    });
    for (Axis axis : filters) {
        String hash = DigestUtils.sha256Hex(axis.getId());
        signature.append("#").append(hash);
    }
    //
    return signature.toString();
}

From source file:com.qwazr.library.realm.table.TableRealmConnector.java

@Override
public Account verify(final String id, final Credential credential) {

    // This realm only support one type of credential
    if (!(credential instanceof PasswordCredential))
        throw new RuntimeException("Unsupported credential type: " + credential.getClass().getName());

    PasswordCredential passwordCredential = (PasswordCredential) credential;

    // We request the database
    final Map<String, ?> row;
    try {// w  ww .j a v  a  2s .c o m
        row = tableService.getRow(table_name, id, columns);
        if (row == null)
            return null;
    } catch (WebApplicationException e) {
        if (e.getResponse().getStatusInfo().getFamily() == Response.Status.Family.CLIENT_ERROR)
            return authenticationFailure("Unknown user: " + id);
        throw e;
    }

    Object password = row.get(password_column);
    if (password == null)
        return null;
    if (password instanceof String[]) {
        String[] passwordArray = (String[]) password;
        if (passwordArray.length == 0)
            return null;
        password = passwordArray[0];
    }

    // The password is stored hashed
    final String passwd = new String(passwordCredential.getPassword());
    String digest = DigestUtils.sha256Hex(passwd);
    if (!digest.equals(password))
        return authenticationFailure("Wrong password: " + id + " " + digest + '/' + passwd + '/' + password);

    //We retrieve the roles
    final Object object = row.get(roles_column);
    final LinkedHashSet<String> roles = new LinkedHashSet<>();
    if (object instanceof String[]) {
        for (Object o : (String[]) object)
            roles.add(o.toString());
    } else
        roles.add(object.toString());

    return new Account() {
        @Override
        public Principal getPrincipal() {
            return () -> id;
        }

        @Override
        public Set<String> getRoles() {
            return roles;
        }
    };
}

From source file:hu.holdinarms.resource.AdminResource.java

/**
 * Change password./*from  ww  w. j  ava 2  s . c om*/
 * 
 * @param admin The admin.
 * @param newPassword The new password.
 * @return The admin with new password.
 */
@PUT
@UnitOfWork
@Path("/changepassword/{newPassword}")
public Admin changePassword(@Auth Admin admin, @PathParam("newPassword") String newPassword) {
    admin.setPassword(DigestUtils.sha256Hex(newPassword));
    return adminDao.save(admin);
}

From source file:io.moquette.spi.impl.security.ResourceAuthenticator.java

@Override
public boolean checkValid(String clientId, String username, byte[] password) {
    if (username == null || password == null) {
        LOG.info("username or password was null");
        return false;
    }/* w w w . j ava  2  s.  c  om*/
    String foundPwq = m_identities.get(username);
    if (foundPwq == null) {
        return false;
    }
    String encodedPasswd = DigestUtils.sha256Hex(password);
    return foundPwq.equals(encodedPasswd);
}

From source file:com.eriwen.gradle.Digest.java

private String digest(final File file, final DigestAlgorithm algorithm) throws IOException {
    final byte[] contentBytes = Files.readAllBytes(file.toPath());
    final String checksum;
    switch (algorithm) {
    case MD5://  w  w  w .j  a va  2  s .c  o  m
        checksum = DigestUtils.md5Hex(contentBytes);
        break;
    case SHA1:
        checksum = DigestUtils.sha1Hex(contentBytes);
        break;
    case SHA256:
        checksum = DigestUtils.sha256Hex(contentBytes);
        break;
    case SHA512:
        checksum = DigestUtils.sha512Hex(contentBytes);
        break;
    default:
        throw new IllegalArgumentException("Cannot use unknown digest algorithm " + algorithm.toString());
    }
    return checksum;
}

From source file:alfio.extension.ExtensionService.java

@Transactional
public void createOrUpdate(String previousPath, String previousName, Extension script) {
    Validate.notBlank(script.getName(), "Name is mandatory");
    Validate.notBlank(script.getPath(), "Path must be defined");
    String hash = DigestUtils.sha256Hex(script.getScript());
    ExtensionMetadata extensionMetadata = getMetadata(script.getName(), script.getScript());

    extensionRepository.deleteEventsForPath(previousPath, previousName);

    if (!Objects.equals(previousPath, script.getPath()) || !Objects.equals(previousName, script.getName())) {
        extensionRepository.deleteScriptForPath(previousPath, previousName);
        extensionRepository.insert(script.getPath(), script.getName(), hash, script.isEnabled(),
                extensionMetadata.async, script.getScript());
    } else {//from  w ww  . j av a  2 s  .  c  o  m
        extensionRepository.update(script.getPath(), script.getName(), hash, script.isEnabled(),
                extensionMetadata.async, script.getScript());
        //TODO: load all saved parameters value, then delete the register extension parameter
    }

    int extensionId = extensionRepository.getExtensionIdFor(script.getPath(), script.getName());

    for (String event : extensionMetadata.events) {
        extensionRepository.insertEvent(extensionId, event);
    }

    //
    ExtensionMetadata.Parameters parameters = extensionMetadata.getParameters();
    if (parameters != null) {
        extensionRepository.deleteExtensionParameter(extensionId);
        //TODO: handle if already present, cleanup key that are no more present
        for (ExtensionMetadata.Field field : parameters.getFields()) {
            for (String level : parameters.getConfigurationLevels()) {
                extensionRepository.registerExtensionConfigurationMetadata(extensionId, field.getName(),
                        field.getDescription(), field.getType(), level, field.isRequired());
                //if for this key,level is present a value -> save
            }
        }
    }
}

From source file:io.muic.ooc.webapp.service.UserService.java

public String hash(String password) {
    String salt = "salt";

    return DigestUtils.sha256Hex(password + salt);
}

From source file:io.lavagna.service.CalendarService.java

@Transactional(readOnly = false)
public CalendarInfo findCalendarInfoFromUser(User user) {
    try {//from  w  ww  . j av  a  2  s  .co  m
        return userRepository.findCalendarInfoFromUserId(user);
    } catch (CalendarTokenNotFoundException ex) {
        String token = UUID.randomUUID().toString();// <- this use secure random
        String hashedToken = DigestUtils.sha256Hex(token);
        userRepository.registerCalendarToken(user, hashedToken);
        return findCalendarInfoFromUser(user);
    }
}

From source file:io.hawkcd.http.AuthController.java

@POST
@Consumes(MediaType.APPLICATION_JSON)/* w  ww  . j  a  v  a2  s .  c o  m*/
@Produces("application/json")
@Path("/login")
public Response login(LoginDto login) throws IOException {
    String hashedPassword = DigestUtils.sha256Hex(login.getPassword());
    ServiceResult serviceResult = this.userService.getByEmailAndPassword(login.getEmail(), hashedPassword);
    if (serviceResult.getNotificationType() == NotificationType.ERROR) {
        return Response.status(Response.Status.BAD_REQUEST).entity(serviceResult).build();
    }

    User userFromDb = (User) serviceResult.getEntity();

    if (!userFromDb.isEnabled()) {
        serviceResult.setNotificationType(NotificationType.ERROR);
        serviceResult.setMessage("Cannot login");
        serviceResult.setEntity(null);
        return Response.status(Response.Status.FORBIDDEN).build();
    }

    String token = TokenAdapter.createJsonWebToken(userFromDb, 30L);

    Gson gson = new Gson();

    String jsonToken = gson.toJson(token);

    return Response.ok(jsonToken).build();
}

From source file:com.arrow.acs.ApiRequestSigner.java

private String hash(String value) {
    return DigestUtils.sha256Hex(value);
}