提交 3b457ffc authored 作者: Thomas Mueller's avatar Thomas Mueller

StringUtils.convertBytesToHex

上级 27d7caa1
......@@ -318,7 +318,7 @@ public class TraceObject {
if (x == null) {
return "null";
}
return "org.h2.util.StringUtils.convertStringToBytes(\"" + StringUtils.convertBytesToString(x) + "\")";
return "org.h2.util.StringUtils.convertHexToBytes(\"" + StringUtils.convertBytesToHex(x) + "\")";
}
/**
......
......@@ -153,13 +153,13 @@ public class CipherFactory {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
store.load(null, password.toCharArray());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
StringUtils.convertStringToBytes("30820277020100300d06092a864886f70d0101010500048202613082025d02010002818100dc0a13c602b7141110eade2f051b54777b060d0f74e6a110f9cce81159f271ebc88d8e8aa1f743b505fc2e7dfe38d33b8d3f64d1b363d1af4d877833897954cbaec2fa384c22a415498cf306bb07ac09b76b001cd68bf77ea0a628f5101959cf2993a9c23dbee79b19305977f8715ae78d023471194cc900b231eecb0aaea98d02030100010281810099aa4ff4d0a09a5af0bd953cb10c4d08c3d98df565664ac5582e494314d5c3c92dddedd5d316a32a206be4ec084616fe57be15e27cad111aa3c21fa79e32258c6ca8430afc69eddd52d3b751b37da6b6860910b94653192c0db1d02abcfd6ce14c01f238eec7c20bd3bb750940004bacba2880349a9494d10e139ecb2355d101024100ffdc3defd9c05a2d377ef6019fa62b3fbd5b0020a04cc8533bca730e1f6fcf5dfceea1b044fbe17d9eababfbc7d955edad6bc60f9be826ad2c22ba77d19a9f65024100dc28d43fdbbc93852cc3567093157702bc16f156f709fb7db0d9eec028f41fd0edcd17224c866e66be1744141fb724a10fd741c8a96afdd9141b36d67fff6309024077b1cddbde0f69604bdcfe33263fb36ddf24aa3b9922327915b890f8a36648295d0139ecdf68c245652c4489c6257b58744fbdd961834a4cab201801a3b1e52d024100b17142e8991d1b350a0802624759d48ae2b8071a158ff91fabeb6a8f7c328e762143dc726b8529f42b1fab6220d1c676fdc27ba5d44e847c72c52064afd351a902407c6e23fe35bcfcd1a662aa82a2aa725fcece311644d5b6e3894853fd4ce9fe78218c957b1ff03fc9e5ef8ffeb6bd58235f6a215c97d354fdace7e781e4a63e8b"));
StringUtils.convertHexToBytes("30820277020100300d06092a864886f70d0101010500048202613082025d02010002818100dc0a13c602b7141110eade2f051b54777b060d0f74e6a110f9cce81159f271ebc88d8e8aa1f743b505fc2e7dfe38d33b8d3f64d1b363d1af4d877833897954cbaec2fa384c22a415498cf306bb07ac09b76b001cd68bf77ea0a628f5101959cf2993a9c23dbee79b19305977f8715ae78d023471194cc900b231eecb0aaea98d02030100010281810099aa4ff4d0a09a5af0bd953cb10c4d08c3d98df565664ac5582e494314d5c3c92dddedd5d316a32a206be4ec084616fe57be15e27cad111aa3c21fa79e32258c6ca8430afc69eddd52d3b751b37da6b6860910b94653192c0db1d02abcfd6ce14c01f238eec7c20bd3bb750940004bacba2880349a9494d10e139ecb2355d101024100ffdc3defd9c05a2d377ef6019fa62b3fbd5b0020a04cc8533bca730e1f6fcf5dfceea1b044fbe17d9eababfbc7d955edad6bc60f9be826ad2c22ba77d19a9f65024100dc28d43fdbbc93852cc3567093157702bc16f156f709fb7db0d9eec028f41fd0edcd17224c866e66be1744141fb724a10fd741c8a96afdd9141b36d67fff6309024077b1cddbde0f69604bdcfe33263fb36ddf24aa3b9922327915b890f8a36648295d0139ecdf68c245652c4489c6257b58744fbdd961834a4cab201801a3b1e52d024100b17142e8991d1b350a0802624759d48ae2b8071a158ff91fabeb6a8f7c328e762143dc726b8529f42b1fab6220d1c676fdc27ba5d44e847c72c52064afd351a902407c6e23fe35bcfcd1a662aa82a2aa725fcece311644d5b6e3894853fd4ce9fe78218c957b1ff03fc9e5ef8ffeb6bd58235f6a215c97d354fdace7e781e4a63e8b"));
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
Certificate[] certs = { CertificateFactory
.getInstance("X.509")
.generateCertificate(
new ByteArrayInputStream(
StringUtils.convertStringToBytes("3082018b3081f502044295ce6b300d06092a864886f70d0101040500300d310b3009060355040313024832301e170d3035303532363133323630335a170d3337303933303036353734375a300d310b300906035504031302483230819f300d06092a864886f70d010101050003818d0030818902818100dc0a13c602b7141110eade2f051b54777b060d0f74e6a110f9cce81159f271ebc88d8e8aa1f743b505fc2e7dfe38d33b8d3f64d1b363d1af4d877833897954cbaec2fa384c22a415498cf306bb07ac09b76b001cd68bf77ea0a628f5101959cf2993a9c23dbee79b19305977f8715ae78d023471194cc900b231eecb0aaea98d0203010001300d06092a864886f70d01010405000381810083f4401a279453701bef9a7681a5b8b24f153f7d18c7c892133d97bd5f13736be7505290a445a7d5ceb75522403e5097515cd966ded6351ff60d5193de34cd36e5cb04d380398e66286f99923fd92296645fd4ada45844d194dfd815e6cd57f385c117be982809028bba1116c85740b3d27a55b1a0948bf291ddba44bed337b9"))), };
StringUtils.convertHexToBytes("3082018b3081f502044295ce6b300d06092a864886f70d0101040500300d310b3009060355040313024832301e170d3035303532363133323630335a170d3337303933303036353734375a300d310b300906035504031302483230819f300d06092a864886f70d010101050003818d0030818902818100dc0a13c602b7141110eade2f051b54777b060d0f74e6a110f9cce81159f271ebc88d8e8aa1f743b505fc2e7dfe38d33b8d3f64d1b363d1af4d877833897954cbaec2fa384c22a415498cf306bb07ac09b76b001cd68bf77ea0a628f5101959cf2993a9c23dbee79b19305977f8715ae78d023471194cc900b231eecb0aaea98d0203010001300d06092a864886f70d01010405000381810083f4401a279453701bef9a7681a5b8b24f153f7d18c7c892133d97bd5f13736be7505290a445a7d5ceb75522403e5097515cd966ded6351ff60d5193de34cd36e5cb04d380398e66286f99923fd92296645fd4ada45844d194dfd815e6cd57f385c117be982809028bba1116c85740b3d27a55b1a0948bf291ddba44bed337b9"))), };
store.setKeyEntry("h2", privateKey, password.toCharArray(), certs);
// --- generated code end ---
return store;
......
......@@ -1263,7 +1263,7 @@ public class WebApp {
} else if (isBuiltIn(sql, "@password_hash")) {
sql = sql.substring("@password_hash".length()).trim();
String[] p = split(sql);
return StringUtils.convertBytesToString(SHA256.getKeyPasswordHash(p[0], p[1].toCharArray()));
return StringUtils.convertBytesToHex(SHA256.getKeyPasswordHash(p[0], p[1].toCharArray()));
} else if (isBuiltIn(sql, "@prof_start")) {
if (profiler != null) {
profiler.stopCollecting();
......
......@@ -198,7 +198,7 @@ public class WebServer implements Service {
private static String generateSessionId() {
byte[] buff = MathUtils.secureRandomBytes(16);
return StringUtils.convertBytesToString(buff);
return StringUtils.convertBytesToHex(buff);
}
/**
......
......@@ -295,7 +295,7 @@ public class FileLock implements Runnable {
private void setUniqueId() {
byte[] bytes = MathUtils.secureRandomBytes(RANDOM_BYTES);
String random = StringUtils.convertBytesToString(bytes);
String random = StringUtils.convertBytesToHex(bytes);
uniqueId = Long.toHexString(System.currentTimeMillis()) + random;
properties.setProperty("id", uniqueId);
}
......
......@@ -283,7 +283,7 @@ public abstract class FileSystem {
if (newRandom || tempRandom == null) {
byte[] prefix = new byte[8];
MathUtils.randomBytes(prefix);
tempRandom = StringUtils.convertBytesToString(prefix) + ".";
tempRandom = StringUtils.convertBytesToHex(prefix) + ".";
}
return tempRandom + tempSequence++;
}
......
......@@ -1095,9 +1095,9 @@ public class Recover extends Tool implements DataHandler {
byte[] passwordHash = SHA256.getHashWithSalt(userPasswordHash, salt);
StringBuilder buff = new StringBuilder();
buff.append("SALT '").
append(StringUtils.convertBytesToString(salt)).
append(StringUtils.convertBytesToHex(salt)).
append("' HASH '").
append(StringUtils.convertBytesToString(passwordHash)).
append(StringUtils.convertBytesToHex(passwordHash)).
append('\'');
byte[] replacement = buff.toString().getBytes();
System.arraycopy(replacement, 0, s.getBytes(), saltIndex, replacement.length);
......
......@@ -937,7 +937,7 @@ public class StringUtils {
* @param s the hex encoded string
* @return the byte array
*/
public static byte[] convertStringToBytes(String s) {
public static byte[] convertHexToBytes(String s) {
int len = s.length();
if (len % 2 != 0) {
throw DbException.get(ErrorCode.HEX_STRING_ODD_1, s);
......@@ -969,8 +969,8 @@ public class StringUtils {
* @param value the byte array
* @return the hex encoded string
*/
public static String convertBytesToString(byte[] value) {
return convertBytesToString(value, value.length);
public static String convertBytesToHex(byte[] value) {
return convertBytesToHex(value, value.length);
}
/**
......@@ -980,7 +980,7 @@ public class StringUtils {
* @param len the number of bytes to encode
* @return the hex encoded string
*/
public static String convertBytesToString(byte[] value, int len) {
public static String convertBytesToHex(byte[] value, int len) {
char[] buff = new char[len + len];
char[] hex = HEX;
for (int i = 0; i < len; i++) {
......@@ -1009,6 +1009,4 @@ public class StringUtils {
return true;
}
}
......@@ -813,9 +813,9 @@ public abstract class Value {
case TIMESTAMP:
return ValueTimestamp.getNoCopy(ValueTimestamp.parseTimestamp(s.trim()));
case BYTES:
return ValueBytes.getNoCopy(StringUtils.convertStringToBytes(s.trim()));
return ValueBytes.getNoCopy(StringUtils.convertHexToBytes(s.trim()));
case JAVA_OBJECT:
return ValueJavaObject.getNoCopy(StringUtils.convertStringToBytes(s.trim()));
return ValueJavaObject.getNoCopy(StringUtils.convertHexToBytes(s.trim()));
case STRING:
return ValueString.get(s);
case STRING_IGNORECASE:
......@@ -829,7 +829,7 @@ public abstract class Value {
case CLOB:
return LobStorage.createSmallLob(CLOB, StringUtils.utf8Encode(s));
case BLOB:
return LobStorage.createSmallLob(BLOB, StringUtils.convertStringToBytes(s.trim()));
return LobStorage.createSmallLob(BLOB, StringUtils.convertHexToBytes(s.trim()));
case ARRAY:
return ValueArray.get(new Value[]{ValueString.get(s)});
case RESULT_SET: {
......
......@@ -83,7 +83,7 @@ public class ValueBytes extends Value {
}
public String getString() {
return StringUtils.convertBytesToString(value);
return StringUtils.convertBytesToHex(value);
}
public long getPrecision() {
......
......@@ -552,7 +552,7 @@ public class ValueLob extends Value {
} else {
buff = IOUtils.readBytesAndClose(getInputStream(), len);
}
return StringUtils.convertBytesToString(buff);
return StringUtils.convertBytesToHex(buff);
} catch (IOException e) {
throw DbException.convertIOException(e, fileName);
}
......@@ -646,7 +646,7 @@ public class ValueLob extends Value {
return StringUtils.quoteStringSQL(s);
}
byte[] buff = getBytes();
s = StringUtils.convertBytesToString(buff);
s = StringUtils.convertBytesToHex(buff);
return "X'" + s + "'";
}
......
......@@ -192,7 +192,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo
} else {
buff = IOUtils.readBytesAndClose(getInputStream(), len);
}
return StringUtils.convertBytesToString(buff);
return StringUtils.convertBytesToHex(buff);
} catch (IOException e) {
throw DbException.convertIOException(e, toString());
}
......@@ -302,7 +302,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo
return StringUtils.quoteStringSQL(s);
}
byte[] buff = getBytes();
s = StringUtils.convertBytesToString(buff);
s = StringUtils.convertBytesToHex(buff);
return "X'" + s + "'";
}
......
......@@ -64,7 +64,7 @@ public class ValueUuid extends Value {
*/
public static ValueUuid get(byte[] binary) {
if (binary.length < 16) {
return get(StringUtils.convertBytesToString(binary));
return get(StringUtils.convertBytesToHex(binary));
}
long high = Utils.readLong(binary, 0);
long low = Utils.readLong(binary, 8);
......
......@@ -152,7 +152,7 @@ public class TestCsv extends TestBase {
private void testPseudoBom() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
// UTF-8 "BOM" / marker
out.write(StringUtils.convertStringToBytes("ef" + "bb" + "bf"));
out.write(StringUtils.convertHexToBytes("ef" + "bb" + "bf"));
out.write("\"ID\", \"NAME\"\n1, Hello".getBytes("UTF-8"));
byte[] buff = out.toByteArray();
Reader r = new InputStreamReader(new ByteArrayInputStream(buff), "UTF-8");
......
......@@ -39,7 +39,7 @@ public class TestSecurity extends TestBase {
private static void testConnectWithHash() throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "sa");
String pwd = StringUtils.convertBytesToString(SHA256.getKeyPasswordHash("SA", "sa".toCharArray()));
String pwd = StringUtils.convertBytesToHex(SHA256.getKeyPasswordHash("SA", "sa".toCharArray()));
Connection conn2 = DriverManager.getConnection("jdbc:h2:mem:test;PASSWORD_HASH=TRUE", "sa", pwd);
conn.close();
conn2.close();
......@@ -54,7 +54,7 @@ public class TestSecurity extends TestBase {
if (data.length > 0) {
assertEquals(0, data[0]);
}
return StringUtils.convertBytesToString(result);
return StringUtils.convertBytesToHex(result);
}
private void testOneSHA() {
......@@ -79,7 +79,7 @@ public class TestSecurity extends TestBase {
}
private void checkSHA256(String message, String expected) {
String hash = StringUtils.convertBytesToString(SHA256.getHash(message.getBytes(), true)).toUpperCase();
String hash = StringUtils.convertBytesToHex(SHA256.getHash(message.getBytes(), true)).toUpperCase();
assertEquals(expected, hash);
}
......@@ -102,28 +102,28 @@ public class TestSecurity extends TestBase {
// http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip
// ECBVarTxt128e.txt
// COUNT = 0
test.setKey(StringUtils.convertStringToBytes("00000000000000000000000000000000"));
data = StringUtils.convertStringToBytes("80000000000000000000000000000000");
test.setKey(StringUtils.convertHexToBytes("00000000000000000000000000000000"));
data = StringUtils.convertHexToBytes("80000000000000000000000000000000");
test.encrypt(data, 0, data.length);
r = StringUtils.convertBytesToString(data);
r = StringUtils.convertBytesToHex(data);
assertEquals("3ad78e726c1ec02b7ebfe92b23d9ec34", r);
// COUNT = 127
test.setKey(StringUtils.convertStringToBytes("00000000000000000000000000000000"));
data = StringUtils.convertStringToBytes("ffffffffffffffffffffffffffffffff");
test.setKey(StringUtils.convertHexToBytes("00000000000000000000000000000000"));
data = StringUtils.convertHexToBytes("ffffffffffffffffffffffffffffffff");
test.encrypt(data, 0, data.length);
r = StringUtils.convertBytesToString(data);
r = StringUtils.convertBytesToHex(data);
assertEquals("3f5b8cc9ea855a0afa7347d23e8d664e", r);
// test vector from
// http://www.inconteam.com/index.php?option=com_content&view=article&id=55:aes-test-vectors&catid=41:encryption&Itemid=60#aes-ecb-128
test.setKey(StringUtils.convertStringToBytes("2b7e151628aed2a6abf7158809cf4f3c"));
data = StringUtils.convertStringToBytes("6bc1bee22e409f96e93d7e117393172a");
test.setKey(StringUtils.convertHexToBytes("2b7e151628aed2a6abf7158809cf4f3c"));
data = StringUtils.convertHexToBytes("6bc1bee22e409f96e93d7e117393172a");
test.encrypt(data, 0, data.length);
r = StringUtils.convertBytesToString(data);
r = StringUtils.convertBytesToHex(data);
assertEquals("3ad77bb40d7a3660a89ecaf32466ef97", r);
test.setKey(StringUtils.convertStringToBytes("000102030405060708090A0B0C0D0E0F"));
test.setKey(StringUtils.convertHexToBytes("000102030405060708090A0B0C0D0E0F"));
byte[] in = new byte[128];
byte[] enc = new byte[128];
test.encrypt(enc, 0, 128);
......
......@@ -41,18 +41,18 @@ public class TestStringUtils extends TestBase {
}
private void testHex() {
assertEquals("face", StringUtils.convertBytesToString(new byte[] { (byte) 0xfa, (byte) 0xce }));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertStringToBytes("face"));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertStringToBytes("fAcE"));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertStringToBytes("FaCe"));
assertEquals("face", StringUtils.convertBytesToHex(new byte[] { (byte) 0xfa, (byte) 0xce }));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertHexToBytes("face"));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertHexToBytes("fAcE"));
assertEquals(new byte[] { (byte) 0xfa, (byte) 0xce }, StringUtils.convertHexToBytes("FaCe"));
try {
StringUtils.convertStringToBytes("120");
StringUtils.convertHexToBytes("120");
fail();
} catch (DbException e) {
assertKnownException(DbException.toSQLException(e));
}
try {
StringUtils.convertStringToBytes("fast");
StringUtils.convertHexToBytes("fast");
fail();
} catch (DbException e) {
assertKnownException(DbException.toSQLException(e));
......
......@@ -44,7 +44,7 @@ public class FileObjectCrypt implements FileObject {
boolean newFile = file.length() < HEADER_LENGTH + BLOCK_SIZE;
byte[] filePasswordHash;
if (algorithm.endsWith("-hash")) {
filePasswordHash = StringUtils.convertStringToBytes(password);
filePasswordHash = StringUtils.convertHexToBytes(password);
algorithm = algorithm.substring(0, algorithm.length() - "-hash".length());
} else {
filePasswordHash = SHA256.getKeyPasswordHash("file", password.toCharArray());
......
......@@ -57,14 +57,14 @@ public class SecureKeyStoreBuilder {
System.out.println("KeyFactory keyFactory = KeyFactory.getInstance(\"" + key.getAlgorithm() + "\");");
System.out.println("store.load(null, password.toCharArray());");
String pkFormat = key.getFormat();
String encoded = StringUtils.convertBytesToString(key.getEncoded());
String encoded = StringUtils.convertBytesToHex(key.getEncoded());
System.out.println(pkFormat + "EncodedKeySpec keySpec = new " + pkFormat + "EncodedKeySpec(getBytes(\""
+ encoded + "\"));");
System.out.println("PrivateKey privateKey = keyFactory.generatePrivate(keySpec);");
System.out.println("Certificate[] certs = {");
for (Certificate cert : store.getCertificateChain(alias)) {
System.out.println(" CertificateFactory.getInstance(\""+cert.getType()+"\").");
String enc = StringUtils.convertBytesToString(cert.getEncoded());
String enc = StringUtils.convertBytesToHex(cert.getEncoded());
System.out.println(" generateCertificate(new ByteArrayInputStream(getBytes(\""+enc+"\"))),");
// PublicKey pubKey = cert.getPublicKey();
// System.out.println(" pubKey algorithm="+pubKey.getAlgorithm());
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论