Search in sources :

Example 1 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class ProductPromoContentWrapper method getProductPromoContentAsText.

public static void getProductPromoContentAsText(String productPromoId, GenericValue productPromo, String productPromoContentTypeId, Locale locale, String mimeTypeId, String partyId, String roleTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws GeneralException, IOException {
    if (UtilValidate.isEmpty(productPromoId) && productPromo != null) {
        productPromoId = productPromo.getString("productPromoId");
    }
    if (UtilValidate.isEmpty(delegator) && productPromo != null) {
        delegator = productPromo.getDelegator();
    }
    if (UtilValidate.isEmpty(mimeTypeId)) {
        mimeTypeId = EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator);
    }
    if (UtilValidate.isEmpty(delegator)) {
        throw new GeneralRuntimeException("Unable to find a delegator to use!");
    }
    List<EntityExpr> exprs = new ArrayList<>();
    exprs.add(EntityCondition.makeCondition("productPromoId", EntityOperator.EQUALS, productPromoId));
    exprs.add(EntityCondition.makeCondition("productPromoContentTypeId", EntityOperator.EQUALS, productPromoContentTypeId));
    List<GenericValue> productPromoContentList = EntityQuery.use(delegator).from("ProductPromoContent").where(EntityCondition.makeCondition(exprs, EntityOperator.AND)).orderBy("-fromDate").cache(cache).queryList();
    GenericValue productPromoContent = null;
    if (UtilValidate.isNotEmpty(productPromoContentList)) {
        productPromoContent = EntityUtil.getFirst(EntityUtil.filterByDate(productPromoContentList));
    }
    if (productPromoContent != null) {
        // when rendering the product promo content, always include the ProductPromo and ProductPromoContent records that this comes from
        Map<String, Object> inContext = new HashMap<>();
        inContext.put("productPromo", productPromo);
        inContext.put("productPromoContent", productPromoContent);
        ContentWorker.renderContentAsText(dispatcher, productPromoContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, partyId, roleTypeId, cache);
        return;
    }
    String candidateFieldName = ModelUtil.dbNameToVarName(productPromoContentTypeId);
    ModelEntity productModel = delegator.getModelEntity("ProductPromo");
    if (productModel.isField(candidateFieldName)) {
        if (UtilValidate.isEmpty(productPromo)) {
            productPromo = EntityQuery.use(delegator).from("ProductPromo").where("productPromoId", productPromoId).cache().queryOne();
        }
        if (productPromo != null) {
            String candidateValue = productPromo.getString(candidateFieldName);
            if (UtilValidate.isNotEmpty(candidateValue)) {
                outWriter.write(candidateValue);
                return;
            }
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 2 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class HashCrypt method getSalt.

private static String getSalt() {
    try {
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        byte[] salt = new byte[16];
        sr.nextBytes(salt);
        return Arrays.toString(salt);
    } catch (NoSuchAlgorithmException e) {
        throw new GeneralRuntimeException("Error while creating salt", e);
    }
}
Also used : GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class HashCrypt method doComparePbkdf2.

public static boolean doComparePbkdf2(String crypted, String password) {
    try {
        int typeEnd = crypted.indexOf("}");
        String hashType = crypted.substring(1, typeEnd);
        String[] parts = crypted.split("\\$");
        int iterations = Integer.parseInt(parts[0].substring(typeEnd + 1));
        byte[] salt = org.apache.ofbiz.base.util.Base64.base64Decode(parts[1]).getBytes(UtilIO.getUtf8());
        byte[] hash = Base64.decodeBase64(parts[2].getBytes(UtilIO.getUtf8()));
        PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, hash.length * 8);
        switch(hashType.substring(hashType.indexOf("-") + 1)) {
            case "SHA256":
                hashType = "PBKDF2WithHmacSHA256";
                break;
            case "SHA384":
                hashType = "PBKDF2WithHmacSHA384";
                break;
            case "SHA512":
                hashType = "PBKDF2WithHmacSHA512";
                break;
            default:
                hashType = "PBKDF2WithHmacSHA1";
        }
        SecretKeyFactory skf = SecretKeyFactory.getInstance(hashType);
        byte[] testHash = skf.generateSecret(spec).getEncoded();
        int diff = hash.length ^ testHash.length;
        for (int i = 0; i < hash.length && i < testHash.length; i++) {
            diff |= hash[i] ^ testHash[i];
        }
        return diff == 0;
    } catch (NoSuchAlgorithmException e) {
        throw new GeneralRuntimeException("Error while computing SecretKeyFactory", e);
    } catch (InvalidKeySpecException e) {
        throw new GeneralRuntimeException("Error while creating SecretKey", e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 4 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class HashCrypt method digestHash.

public static String digestHash(String hashType, byte[] bytes) {
    try {
        MessageDigest messagedigest = MessageDigest.getInstance(hashType);
        messagedigest.update(bytes);
        byte[] digestBytes = messagedigest.digest();
        char[] digestChars = Hex.encodeHex(digestBytes);
        StringBuilder sb = new StringBuilder();
        sb.append("{").append(hashType).append("}");
        sb.append(digestChars, 0, digestChars.length);
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e);
    }
}
Also used : GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 5 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class HashCrypt method getCryptedBytes.

private static String getCryptedBytes(String hashType, String salt, byte[] bytes) {
    try {
        MessageDigest messagedigest = MessageDigest.getInstance(hashType);
        messagedigest.update(salt.getBytes(UtilIO.getUtf8()));
        messagedigest.update(bytes);
        return Base64.encodeBase64URLSafeString(messagedigest.digest()).replace('+', '.');
    } catch (NoSuchAlgorithmException e) {
        throw new GeneralRuntimeException("Error while comparing password", e);
    }
}
Also used : GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)20 GenericValue (org.apache.ofbiz.entity.GenericValue)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)6 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)6 HashMap (java.util.HashMap)5 MessageDigest (java.security.MessageDigest)3 ArrayList (java.util.ArrayList)3 Locale (java.util.Locale)3 Delegator (org.apache.ofbiz.entity.Delegator)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 SQLException (java.sql.SQLException)2 SecretKeyFactory (javax.crypto.SecretKeyFactory)2 PBEKeySpec (javax.crypto.spec.PBEKeySpec)2 GeneralException (org.apache.ofbiz.base.util.GeneralException)2 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)2 TemplateException (freemarker.template.TemplateException)1 Writer (java.io.Writer)1