import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
-import java.util.Calendar;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
-import javax.xml.bind.DatatypeConverter;
-
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.util.CodePageUtil;
import org.apache.poi.util.HexDump;
String str = String.format(Locale.ROOT, "%02d:%02d:%02d.%03d",hr,min,sec,ms);
b.append(str);
} else {
- Calendar cal = Calendar.getInstance(LocaleUtil.TIMEZONE_UTC, Locale.ROOT);
- cal.setTime(d);
// use ISO-8601 timestamp format
- b.append(DatatypeConverter.printDateTime(cal));
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT);
+ df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
+ b.append(df.format(d));
}
} else if (type == Variant.VT_EMPTY || type == Variant.VT_NULL || value == null) {
b.append("null");
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.util.ArrayList;
+import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
-import javax.xml.bind.DatatypeConverter;
import javax.xml.crypto.MarshalException;
import javax.xml.crypto.URIDereferencer;
import javax.xml.crypto.XMLStructure;
import org.apache.jcp.xml.dsig.internal.dom.DOMSignedInfo;
import org.apache.jcp.xml.dsig.internal.dom.DOMSubTreeData;
import org.apache.poi.EncryptedDocumentException;
+import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.poifs.crypt.dsig.SignatureConfig.SignatureConfigurable;
import org.apache.poi.poifs.crypt.dsig.facets.SignatureFacet;
import org.apache.poi.poifs.crypt.dsig.services.RelationshipTransformService;
-import org.apache.poi.ooxml.util.DocumentHelper;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.xml.security.Init;
-import org.apache.xml.security.utils.Base64;
import org.apache.xml.security.utils.XMLUtils;
import org.apache.xmlbeans.XmlOptions;
import org.w3.x2000.x09.xmldsig.SignatureDocument;
final PrivateKey key = signatureConfig.getKey();
final HashAlgorithm algo = signatureConfig.getDigestAlgo();
- if (algo.hashSize*4/3 > Base64.BASE64DEFAULTLENGTH && !XMLUtils.ignoreLineBreaks()) {
+ // taken from org.apache.xml.security.utils.Base64
+ final int BASE64DEFAULTLENGTH = 76;
+
+
+ if (algo.hashSize*4/3 > BASE64DEFAULTLENGTH && !XMLUtils.ignoreLineBreaks()) {
throw new EncryptedDocumentException("The hash size of the choosen hash algorithm ("+algo+" = "+algo.hashSize+" bytes), "+
"will motivate XmlSec to add linebreaks to the generated digest, which results in an invalid signature (... at least "+
"for Office) - please persuade it otherwise by adding '-Dorg.apache.xml.security.ignoreLineBreaks=true' to the JVM "+
final DOMSubTreeData subTree = new DOMSubTreeData(el, true);
signedInfo.getCanonicalizationMethod().transform(subTree, xmlSignContext, dos);
- return DatatypeConverter.printBase64Binary(dos.sign());
+ return Base64.getEncoder().encodeToString(dos.sign());
} catch (GeneralSecurityException|IOException|TransformException e) {
throw new EncryptedDocumentException(e);
}
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
+import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import javax.xml.bind.DatatypeConverter;
-
import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.poifs.crypt.dsig.SignatureConfig;
try {
if (signatureConfig.getTspUser() != null) {
String userPassword = signatureConfig.getTspUser() + ":" + signatureConfig.getTspPass();
- String encoding = DatatypeConverter.printBase64Binary(userPassword.getBytes(StandardCharsets.ISO_8859_1));
+ String encoding = Base64.getEncoder().encodeToString(userPassword.getBytes(StandardCharsets.ISO_8859_1));
huc.setRequestProperty("Authorization", "Basic " + encoding);
}
import java.security.SecureRandom;
import java.util.Arrays;
+import java.util.Base64;
import java.util.Locale;
-import javax.xml.bind.DatatypeConverter;
import javax.xml.namespace.QName;
import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.util.Internal;
-import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
// --> In this third stage, the reversed byte order legacy hash from the second stage shall
// be converted to Unicode hex string representation
byte[] hash = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCount, false);
-
+
+ Base64.Encoder enc64 = Base64.getEncoder();
+
cur.insertAttributeWithValue(getAttrName(prefix, "algorithmName"), hashAlgo.jceId);
- cur.insertAttributeWithValue(getAttrName(prefix, "hashValue"), DatatypeConverter.printBase64Binary(hash));
- cur.insertAttributeWithValue(getAttrName(prefix, "saltValue"), DatatypeConverter.printBase64Binary(salt));
+ cur.insertAttributeWithValue(getAttrName(prefix, "hashValue"), enc64.encodeToString(hash));
+ cur.insertAttributeWithValue(getAttrName(prefix, "saltValue"), enc64.encodeToString(salt));
cur.insertAttributeWithValue(getAttrName(prefix, "spinCount"), ""+spinCount);
}
cur.dispose();
return false;
}
- byte[] hash1 = DatatypeConverter.parseBase64Binary(hashVal);
+ Base64.Decoder dec64 = Base64.getDecoder();
+
+ byte[] hash1 = dec64.decode(hashVal);
HashAlgorithm hashAlgo = HashAlgorithm.fromString(algoName);
- byte[] salt = DatatypeConverter.parseBase64Binary(saltVal);
+ byte[] salt = dec64.decode(saltVal);
int spinCnt = Integer.parseInt(spinCount);
byte[] hash2 = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCnt, false);
return Arrays.equals(hash1, hash2);
import static org.junit.Assert.assertEquals;
import java.io.IOException;
-
-import javax.xml.bind.DatatypeConverter;
+import java.util.Base64;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.junit.Ignore;
for (final String[] check : checks) {
final HashAlgorithm ha = HashAlgorithm.valueOf(check[0]);
try (final DigestOutputStream dos = new DigestOutputStream(ha, null)) {
- final String magic = DatatypeConverter.printBase64Binary(dos.getHashMagic());
+ final String magic = Base64.getEncoder().encodeToString(dos.getHashMagic());
assertEquals("hash digest magic mismatches", check[1], magic);
}
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
+import java.util.Base64;
import java.util.List;
-import javax.xml.bind.DatatypeConverter;
-
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.HSSFTestDataSamples;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(input);
- return DatatypeConverter.printBase64Binary(hash);
+ return Base64.getEncoder().encodeToString(hash);
} catch (NoSuchAlgorithmException e) {
// doesn't happen
throw new RuntimeException(e);
#@defaultMessage Deprecated Java APIs\r
#java.lang.StringBuffer\r
#java.util.Hashtable\r
+\r
+@defaultMessage DatatypeConverter is not available in Java 9+ without adding add-opens - use java.util.Base64\r
+javax.xml.bind.DatatypeConverter
\ No newline at end of file
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Base64;
import java.util.zip.GZIPInputStream;
-import javax.xml.bind.DatatypeConverter;
-
import org.apache.poi.util.HexDump;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.IOUtils;
* @throws IOException if you copy and pasted the data wrong
*/
public static byte[] decompress(String data) throws IOException {
- byte[] base64Bytes = DatatypeConverter.parseBase64Binary(data);
+ byte[] base64Bytes = Base64.getDecoder().decode(data);
return IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(base64Bytes)));
}
java.util.zip.GZIPOutputStream gz = new java.util.zip.GZIPOutputStream(bos);
gz.write(data);
gz.finish();
- return DatatypeConverter.printBase64Binary(bos.toByteArray());
+ return Base64.getEncoder().encodeToString(bos.toByteArray());
}
}