Skip to content

Commit

Permalink
Use Shape Base64Value class for "integrity" check
Browse files Browse the repository at this point in the history
  • Loading branch information
sideshowbarker committed Dec 31, 2015
1 parent 843f3a5 commit 41bbc6f
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/nu/validator/datatype/IntegrityMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import org.relaxng.datatype.DatatypeException;

import com.shapesecurity.salvation.data.Base64Value;

public final class IntegrityMetadata extends AbstractDatatype {

/**
Expand All @@ -37,10 +39,6 @@ public final class IntegrityMetadata extends AbstractDatatype {
private static final Pattern THE_PATTERN = Pattern.compile(
"^(?:sha256-|sha384-|sha512)(.+$)", Pattern.CASE_INSENSITIVE);

private static final Pattern FULL_PATTERN = Pattern.compile(
"^(?:sha256-|sha384-|sha512-)(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$",
Pattern.CASE_INSENSITIVE);

private IntegrityMetadata() {
super();
}
Expand All @@ -51,11 +49,10 @@ private IntegrityMetadata() {
public void checkValid(CharSequence literal) throws DatatypeException {
Matcher m = getPattern().matcher(literal);
if (m.matches()) {
Matcher n = getFullPattern().matcher(literal);
if (WARN && !n.matches()) {
throw newDatatypeException("The text \u201c" + m.group(1)
+ "\u201d does not appear to be a valid"
+ " base64-encoded string.", WARN);
try {
new Base64Value(m.group(1));
} catch (IllegalArgumentException e) {
throw newDatatypeException(e.getMessage(), WARN);
}
} else {
throw newDatatypeException("The value must start with"
Expand All @@ -74,16 +71,6 @@ protected Pattern getPattern() {
return THE_PATTERN;
}

/**
* Returns the full regexp for this datatype.
*
* @return the full regexp for this datatype
* @see nu.validator.datatype.AbstractDatetime#getPattern()
*/
protected Pattern getFullPattern() {
return FULL_PATTERN;
}

@Override public String getName() {
return "integrity-metadata";
}
Expand Down

0 comments on commit 41bbc6f

Please sign in to comment.