Example usage for org.springframework.security.crypto.codec Hex decode

List of usage examples for org.springframework.security.crypto.codec Hex decode

Introduction

In this page you can find the example usage for org.springframework.security.crypto.codec Hex decode.

Prototype

public static byte[] decode(CharSequence s) 

Source Link

Usage

From source file:de.inren.service.picture.PictureModificationServiceImpl.java

public void initPictureModificationService() {
    Application.get().getSharedResources().add(PictureResource.PICTURE_RESOURCE, new PictureResource());
    final File file = storehouseService.getDataFile(PictureModificationService.FILE_NOT_FOUND_DIGEST);
    if (!file.exists()) {
        InputStream is = PictureModificationServiceImpl.class
                .getResourceAsStream(PictureModificationService.FILE_NOT_FOUND_NAME);
        final Hex hex = new Hex();
        byte[] md5sum;
        md5sum = (byte[]) hex.decode(PictureModificationService.FILE_NOT_FOUND_DIGEST);
        // prescale to thumbnail, also test for converter
        storehouseService.doImport(is, md5sum);
        File thumbFile = getThumbnailImage(PictureModificationService.FILE_NOT_FOUND_DIGEST);
        log.info("picture modification service \"File not found\" image imported");
        try {/*from w ww .j  av a  2 s . c  om*/
            log.info("Mimetype of " + thumbFile.getName() + " is " + Files.probeContentType(file.toPath()));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

    } else {
        try {
            log.info("Mimetype of " + PictureModificationService.FILE_NOT_FOUND_NAME + " is "
                    + Files.probeContentType(file.toPath()));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.matrix.security.crypto.encrypt.HexEncodingTextEncryptor.java

public String decrypt(String encryptedText) {
    return Utf8.decode(encryptor.decrypt(Hex.decode(encryptedText)));
}

From source file:com.greensopinion.finance.services.encryption.Encryptor.java

public String decrypt(String data) {
    if (data == null) {
        return null;
    }/*from   ww w.ja va2s  .  co m*/
    byte[] decodedBytes = Hex.decode(data);
    byte[] decryptedBytes = encryptor.decrypt(decodedBytes);
    return new String(decryptedBytes, StandardCharsets.UTF_8);
}

From source file:org.matrix.security.crypto.encrypt.AesBytesEncryptor.java

public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) {
    PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256);
    SecretKey secretKey = newSecretKey("PBKDF2WithHmacSHA1", keySpec);
    this.secretKey = new SecretKeySpec(secretKey.getEncoded(), "AES");
    encryptor = newCipher(AES_ALGORITHM);
    decryptor = newCipher(AES_ALGORITHM);
    this.ivGenerator = ivGenerator != null ? ivGenerator : NULL_IV_GENERATOR;
}

From source file:io.pivotal.cla.security.GitHubSignature.java

@SneakyThrows
public boolean check(String gitHubSignature, String body) {
    if (gitHubSignature == null || !gitHubSignature.startsWith(SIGNATURE_PREFIX)) {
        return false;
    }//from  ww  w  .  j a  v a 2 s .  c  om
    AccessToken expectedToken = accessTokens.findOne(AccessToken.CLA_ACCESS_TOKEN_ID);
    if (expectedToken == null) {
        return false;
    }

    String providedHmac = gitHubSignature.substring(SIGNATURE_PREFIX.length());

    byte[] providedHmacBytes = Hex.decode(providedHmac);

    byte[] expectedBytes = sign(body, expectedToken.getToken());

    return MessageDigest.isEqual(providedHmacBytes, expectedBytes);
}

From source file:org.matrix.security.crypto.password.StandardPasswordEncoder.java

private byte[] decode(CharSequence encodedPassword) {
    return Hex.decode(encodedPassword);
}

From source file:com.springsource.greenhouse.account.GreenhousePasswordEncoder.java

private byte[] decode(String encodedPassword) {
    return Hex.decode(encodedPassword);
}

From source file:org.cloudfoundry.identity.uaa.authentication.RubyUserTokenTests.java

/**
 *
 * @param token the token string containing the ruby-marshalled username, expiry time and HMAC signature
 * @param key Used to validate the signature of the token.
 *
 * @return a UserToken instance containing the data
 * @throws SignatureException if the signature in the token string does not match the one calculated using the
 *        supplied key.//from ww  w .  j a va2 s . c om
 */
public static UserToken decode(String token, SecretKey key) throws SignatureException {
    ByteArrayInputStream bytes = new ByteArrayInputStream(Hex.decode(token));

    try {
        UnmarshalStream stream = new UnmarshalStream(Ruby.getGlobalRuntime(), bytes, null, false);
        IRubyObject object = stream.unmarshalObject();

        Assert.isInstanceOf(RubyArray.class, object);

        RubyArray array = (RubyArray) object;

        Assert.isTrue(array.size() == 3);

        String username = (String) array.get(0);
        Long validUntil = (Long) array.get(1);
        ByteList sigBytes = ((RubyString) array.eltOk(2)).getByteList();

        //         HexDumpEncoder enc = new HexDumpEncoder();
        //         System.out.println("Signature from token is: \n" + enc.encode(sigBytes.unsafeBytes()));

        UserToken ut = new UserToken(username, validUntil, key);

        if (!Arrays.equals(ut.signature, sigBytes.unsafeBytes())) {
            throw new SignatureException(
                    "Signature is invalid for username = " + username + ", validUntil " + validUntil);
        }

        return ut;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.apache.syncope.core.util.ImportExport.java

private void setParameters(final String tableName, final Attributes attrs, final Query query) {

    Map<String, Integer> colTypes = new HashMap<String, Integer>();

    final Table table = getTable(tableName);

    for (int i = 0; i < attrs.getLength(); i++) {
        Integer colType = table.getColumn(QualifiedDBIdentifier.newColumn(attrs.getQName(i))).getType();
        if (colType == null) {
            LOG.warn("No column type found for {}", attrs.getQName(i).toUpperCase());
            colType = Types.VARCHAR;
        }/* ww  w.  j  av  a 2  s.  c  om*/

        switch (colType) {
        case Types.INTEGER:
        case Types.TINYINT:
        case Types.SMALLINT:
            try {
                query.setParameter(i + 1, Integer.valueOf(attrs.getValue(i)));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Integer '{}'", attrs.getValue(i));
                query.setParameter(i + 1, attrs.getValue(i));
            }
            break;

        case Types.NUMERIC:
        case Types.DECIMAL:
        case Types.BIGINT:
            try {
                query.setParameter(i + 1, Long.valueOf(attrs.getValue(i)));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Long '{}'", attrs.getValue(i));
                query.setParameter(i + 1, attrs.getValue(i));
            }
            break;

        case Types.DOUBLE:
            try {
                query.setParameter(i + 1, Double.valueOf(attrs.getValue(i)));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Double '{}'", attrs.getValue(i));
                query.setParameter(i + 1, attrs.getValue(i));
            }
            break;

        case Types.REAL:
        case Types.FLOAT:
            try {
                query.setParameter(i + 1, Float.valueOf(attrs.getValue(i)));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Float '{}'", attrs.getValue(i));
                query.setParameter(i + 1, attrs.getValue(i));
            }
            break;

        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            try {
                query.setParameter(i + 1,
                        DateUtils.parseDate(attrs.getValue(i), SyncopeConstants.DATE_PATTERNS),
                        TemporalType.TIMESTAMP);
            } catch (ParseException e) {
                LOG.error("Unparsable Date '{}'", attrs.getValue(i));
                query.setParameter(i + 1, attrs.getValue(i));
            }
            break;

        case Types.BIT:
        case Types.BOOLEAN:
            query.setParameter(i + 1, "1".equals(attrs.getValue(i)) ? Boolean.TRUE : Boolean.FALSE);
            break;

        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            try {
                query.setParameter(i + 1, Hex.decode(attrs.getValue(i)));
            } catch (IllegalArgumentException e) {
                query.setParameter(i + 1, attrs.getValue(i));
            }
            break;

        case Types.BLOB:
            try {
                query.setParameter(i + 1, Hex.decode(attrs.getValue(i)));
            } catch (IllegalArgumentException e) {
                LOG.warn("Error decoding hex string to specify a blob parameter", e);
                query.setParameter(i + 1, attrs.getValue(i));
            } catch (Exception e) {
                LOG.warn("Error creating a new blob parameter", e);
            }
            break;

        default:
            query.setParameter(i + 1, attrs.getValue(i));
        }
    }
}

From source file:org.apache.syncope.core.provisioning.java.propagation.LDAPPasswordPropagationActions.java

@Transactional(readOnly = true)
@Override/* w w  w  .  j a  v  a 2s.  c  o m*/
public void before(final PropagationTask task, final ConnectorObject beforeObj) {
    super.before(task, beforeObj);

    if (AnyTypeKind.USER == task.getAnyTypeKind()) {
        User user = userDAO.find(task.getEntityKey());

        if (user != null && user.getPassword() != null) {
            Attribute missing = AttributeUtil.find(PropagationTaskExecutor.MANDATORY_MISSING_ATTR_NAME,
                    task.getAttributes());

            ConnInstance connInstance = task.getResource().getConnector();
            String cipherAlgorithm = getCipherAlgorithm(connInstance);
            if (missing != null && missing.getValue() != null && missing.getValue().size() == 1
                    && missing.getValue().get(0).equals(OperationalAttributes.PASSWORD_NAME)
                    && cipherAlgorithmMatches(getCipherAlgorithm(connInstance), user.getCipherAlgorithm())) {

                String password = user.getPassword().toLowerCase();
                byte[] decodedPassword = Hex.decode(password);
                byte[] base64EncodedPassword = Base64.encode(decodedPassword);

                String cipherPlusPassword = ("{" + cipherAlgorithm.toLowerCase() + "}"
                        + new String(base64EncodedPassword));

                Attribute passwordAttribute = AttributeBuilder
                        .buildPassword(new GuardedString(cipherPlusPassword.toCharArray()));

                Set<Attribute> attributes = new HashSet<>(task.getAttributes());
                attributes.add(passwordAttribute);
                attributes.remove(missing);

                task.setAttributes(attributes);
            }
        }
    }
}