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;
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations