下面列出了 org.apache.commons.codec.binary.Base64 # isBase64 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Extracts data without prefix since data from the front end is based64-encoded and prefixed with
* "data:application/x-pkcs12;base64,".
*
* @param base64String from client
* @return extracted data without prefix
*/
private Text removeClientHeaderFromData(String base64String) {
if (StringUtility.isNullOrEmpty(base64String)) {
return null;
}
int index = base64String.indexOf(PKCS12_BASE64_PREFIX);
if (index < 0) {
return null;
}
String data = base64String.substring(index + PKCS12_BASE64_PREFIX.length());
if (Base64.isBase64(data)) {
return new Text(data);
} else {
return null;
}
}
@Override
public ValidationResult validateKey(String keyValue) {
if (StringUtils.isBlank(keyValue)) {
return new ValidationResult.Builder()
.subject("Key Material")
.valid(false)
.explanation("it is empty")
.build();
}
byte[] keyMaterial;
try {
if (!Base64.isBase64(keyValue)) {
throw new Exception();
}
keyMaterial = Base64.decodeBase64(keyValue);
} catch (Exception e) {
return new ValidationResult.Builder()
.subject("Key Material")
.valid(false)
.explanation("it is not in Base64 encoded form")
.build();
}
if (!(keyMaterial.length == 32 || keyMaterial.length == 24 || keyMaterial.length == 16)) {
return new ValidationResult.Builder()
.subject("Key Material")
.valid(false)
.explanation("it is not a Base64 encoded AES-256, AES-192 or AES-128 key")
.build();
}
return new ValidationResult.Builder().valid(true).build();
}
@Override
public int read(byte[] b) throws IOException {
int numRead = super.read(b);
if (numRead > 0) {
byte[] copy = b;
if (numRead < b.length) {
// isBase64 checks the whole length of byte[], we need to limit it to numRead
copy = Arrays.copyOf(b, numRead);
}
if (!Base64.isBase64(copy)) {
throw new IOException("Data is not base64 encoded.");
}
}
return numRead;
}
@Override
public int read(byte[] b, int offset, int len) throws IOException {
int numRead = super.read(b, offset, len);
if (numRead > 0) {
byte[] copy = b;
if (numRead < b.length) {
// isBase64 checks the whole length of byte[], we need to limit it to numRead
copy = Arrays.copyOf(b, numRead);
}
if (!Base64.isBase64(copy)) {
throw new IOException("Data is not base64 encoded.");
}
}
return numRead;
}
@Override
public int read(byte[] b) throws IOException {
int numRead = super.read(b);
if (numRead > 0) {
byte[] copy = b;
if (numRead < b.length) {
// isBase64 checks the whole length of byte[], we need to limit it to numRead
copy = Arrays.copyOf(b, numRead);
}
if (!Base64.isBase64(copy)) {
throw new IOException("Data is not base64 encoded.");
}
}
return numRead;
}
@Override
public int read() throws IOException {
int data = super.read();
if (!Base64.isBase64((byte) data)) {
throw new IOException("Data is not base64 encoded.");
}
return super.read();
}
@Override
public int read(byte[] b, int offset, int len) throws IOException {
int numRead = super.read(b, offset, len);
if (numRead > 0) {
byte[] copy = b;
if (numRead < b.length) {
// isBase64 checks the whole length of byte[], we need to limit it to numRead
copy = Arrays.copyOf(b, numRead);
}
if (!Base64.isBase64(copy)) {
throw new IOException("Data is not base64 encoded.");
}
}
return numRead;
}
public String base64DecodeScript(String encodedScript) {
if (StringUtils.isBlank(encodedScript)) {
return "";
}
try {
String decodedScript = Base64.isBase64(encodedScript) ? Utils.base64dec(encodedScript) : "";
return StringUtils.isBlank(decodedScript) ? encodedScript : decodedScript;
} catch (Exception e) {
return encodedScript;
}
}
public static String decodeBase64(String base64String) {
if (base64String != null && base64String.length() > 0 && Base64.isBase64(base64String)) {
try {
return new String(Base64.decodeBase64(base64String), CommonUtil.BASE64_CHARSET);
} catch (UnsupportedEncodingException e) {
}
}
return "";
}
private Object deSerializeValue(String value, String type, boolean secured) throws IllegalClassException {
if (value == null || type == null) {
return null;
}
String string;
if (secured) {
// sanity check should be Base64 encoded
if (Base64.isBase64(value)) {
string = textEncryptor.decrypt(value);
} else {
log.warn("Invalid value found attempting to decrypt a secured property, check your secured properties");
string = value;
}
} else {
string = value;
}
Object obj;
if (ServerConfigurationService.TYPE_STRING.equals(type)) {
obj = string;
} else if (ServerConfigurationService.TYPE_INT.equals(type)) {
obj = Integer.valueOf(string);
} else if (ServerConfigurationService.TYPE_BOOLEAN.equals(type)) {
obj = Boolean.valueOf(string);
} else if (ServerConfigurationService.TYPE_ARRAY.equals(type)) {
obj = string.split(HibernateConfigItem.ARRAY_SEPARATOR);
} else {
throw new IllegalClassException("deSerializeValue() invalid TYPE, while deserializing");
}
return obj;
}
@Override
protected Field processField(Record record, byte[] fieldData) throws OnRecordErrorException {
if (!Base64.isBase64(fieldData)) {
throw new OnRecordErrorException(DataFormatErrors.DATA_FORMAT_302, record.toString());
}
return Field.create(Base64.decodeBase64(fieldData));
}
public static byte[] base64decodeByteArray(String base64Str) throws Base64Exception {
if(!Base64.isBase64(base64Str)) {
throw new Base64Exception("Provided string is not Base64 encoded");
}
byte[] out = Base64.decodeBase64(base64Str);
return out;
}
public Result viewableDownload(
String derivitiveURN,
String dirName
)
throws IOException,
URISyntaxException
{
String urn;
String base64urn;
if( Base64.isBase64(derivitiveURN) )
{
urn = new String( Base64.decodeBase64( derivitiveURN ) );
base64urn = derivitiveURN;
}
else
{
urn = derivitiveURN;
base64urn = new String( Base64.encodeBase64URLSafe( derivitiveURN.getBytes() ));
}
ResultViewerService result = _service.viewableQuery( urn );
if( result.isError() )
{
return result;
}
List<String> files = new LinkedList<String>();
result.listDerivativeFiles( files );
Iterator<String> itr = files.iterator();
while( itr.hasNext() )
{
String fileName = itr.next();
Result dr = _service.viewableDownload( base64urn, dirName, fileName );
if( dr.isError() )
{
System.out.println( dr.toString() );
}
else
{
System.out.println( dirName + "/" + fileName );;
}
}
return result;
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return !StringUtils.hasLength(value) || Base64.isBase64(value);
}
public static boolean isBase64( String str )
{
return Base64.isBase64( str );
}
@Override
public boolean isBase64Encoded(String base64String) {
return Base64.isBase64(base64String);
}
/**
* Checks if the passed in String is a valid {@link Base64} encoded String. Will throw a {@link DataFormatException} if not
* formatted correctly.
*
* @param toCheck {@link String} to check if valid {@link Base64}
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}
/**
* Checks if the passed in String is a valid {@link Base64} encoded String. Will throw a {@link DataFormatException} if not
* formatted correctly.
*
* @param toCheck {@link String} to check if valid {@link Base64}
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}
/**
* Checks if the passed in String is a valid {@link Base64} encoded String. Will throw a {@link DataFormatException} if not
* formatted correctly.
*
* @param toCheck {@link String} to check if valid {@link Base64}
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}
/**
* Checks if the passed in String is a valid {@link Base64} encoded String. Will throw a {@link DataFormatException} if not
* formatted correctly.
*
* @param toCheck {@link String} to check if valid {@link Base64}
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}