Replaces instances of getBytes() in core classes

This commit is contained in:
z3r0fox
2015-12-20 01:18:38 +00:00
parent 0b94d866f0
commit c60f3970d1
26 changed files with 57 additions and 49 deletions

View File

@ -9,6 +9,8 @@ import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.security.NoSuchAlgorithmException;
import net.i2p.data.DataHelper;
/**
* Class for generation and parsing of <a href="http://www.hashcash.org/">HashCash</a><br>
* Copyright 2006 Gregory Rubin <a href="mailto:grrubin@gmail.com">grrubin@gmail.com</a><br>
@ -61,7 +63,7 @@ public class HashCash implements Comparable<HashCash> {
myExtensions = deserializeExtensions(parts[index++]);
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(cash.getBytes());
md.update(DataHelper.getUTF8(cash));
byte[] tempBytes = md.digest();
int tempValue = numberOfLeadingZeros(tempBytes);
@ -190,7 +192,7 @@ public class HashCash implements Comparable<HashCash> {
serializeExtensions(extensions) + ":";
result.myToken = generateCash(prefix, value, md);
md.reset();
md.update(result.myToken.getBytes());
md.update(DataHelper.getUTF8(result.myToken));
result.myValue = numberOfLeadingZeros(md.digest());
break;
@ -294,7 +296,7 @@ public class HashCash implements Comparable<HashCash> {
counter++;
temp = prefix + Long.toHexString(counter);
md.reset();
md.update(temp.getBytes());
md.update(DataHelper.getUTF8(temp));
bArray = md.digest();
tempValue = numberOfLeadingZeros(bArray);
} while ( tempValue < value);

View File

@ -10,6 +10,7 @@ import java.io.IOException;
import java.util.Locale;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
/**
@ -57,7 +58,7 @@ public class EepGetAndAddNamingService extends EepGetNamingService {
try {
fos = new FileOutputStream(f, true);
String line = hostname + '=' + rv.toBase64() + System.getProperty("line.separator");
fos.write(line.getBytes());
fos.write(DataHelper.getASCII(line));
} catch (IOException ioe) {
System.err.println("Error appending: " + ioe);
} finally {

View File

@ -72,7 +72,7 @@ public class Base32 {
private static void runApp(String args[]) {
String cmd = args[0].toLowerCase(Locale.US);
if ("encodestring".equals(cmd)) {
System.out.println(encode(args[1].getBytes()));
System.out.println(encode(DataHelper.getUTF8(args[1])));
return;
}
InputStream in = System.in;
@ -143,7 +143,7 @@ public class Base32 {
* @param source if null will return ""
*/
public static String encode(String source) {
return (source != null ? encode(source.getBytes()) : "");
return (source != null ? encode(DataHelper.getUTF8(source)) : "");
}
/**
@ -210,7 +210,7 @@ public class Base32 {
* @return decoded data, null on error
*/
public static byte[] decode(String s) {
return decode(s.getBytes());
return decode(DataHelper.getASCII(s));
}
private final static byte[] dmask = { (byte) 0xf8, (byte) 0x7c, (byte) 0x3e, (byte) 0x1f,

View File

@ -385,7 +385,7 @@ public class PrivateKeyFile {
hcs = hcs.substring(0, end1) + hcs.substring(start2);
System.out.println("Short Hashcash is: " + hcs);
c.setPayload(hcs.getBytes());
c.setPayload(DataHelper.getUTF8(hcs));
return c;
}

View File

@ -114,7 +114,7 @@ public class HexDump {
}
}
out.write(" |".getBytes());
out.write(DataHelper.getASCII(" |"));
for (i = 0; i < FORMAT_BYTES_PER_ROW; ++i) {
if (i >= nextbytes) {

View File

@ -43,15 +43,15 @@ public class DatagramTest extends TestCase {
I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
I2PDatagramMaker dm = new I2PDatagramMaker(session);
byte[] dg = dm.makeI2PDatagram("What's the deal with 42?".getBytes());
byte[] dg = dm.makeI2PDatagram(DataHelper.getASCII("What's the deal with 42?"));
I2PDatagramDissector dd = new I2PDatagramDissector();
dd.loadI2PDatagram(dg);
byte[] x = dd.getPayload();
assertTrue(DataHelper.eq(x, "What's the deal with 42?".getBytes()));
assertTrue(DataHelper.eq(x, DataHelper.getASCII("What's the deal with 42?")));
x = dd.extractPayload();
assertTrue(DataHelper.eq(x, "What's the deal with 42?".getBytes()));
assertTrue(DataHelper.eq(x, DataHelper.getASCII("What's the deal with 42?")));
assertEquals(d, dd.getSender());
assertEquals(d, dd.extractSender());
@ -67,7 +67,7 @@ public class DatagramTest extends TestCase {
ByteArrayOutputStream dout = new ByteArrayOutputStream();
d.writeBytes(dout);
dsaEng.sign(Hash.FAKE_HASH.toByteArray(), session.getPrivateKey()).writeBytes(dout);
dout.write("blah".getBytes());
dout.write(DataHelper.getASCII("blah"));
byte[] data = dout.toByteArray();
I2PDatagramDissector dd = new I2PDatagramDissector();

View File

@ -19,6 +19,7 @@ import net.i2p.client.I2PClientFactory;
import net.i2p.client.I2PSession;
import net.i2p.client.I2PSessionException;
import net.i2p.client.I2PSessionListener;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
@ -66,10 +67,10 @@ public class I2PSessionTest extends TestCase implements I2PSessionListener{
_s.add("c");
_s.add("d");
session.sendMessage(d, "a".getBytes());
session.sendMessage(d, "b".getBytes());
session.sendMessage(d, "c".getBytes());
session.sendMessage(d, "d".getBytes());
session.sendMessage(d, DataHelper.getASCII("a"));
session.sendMessage(d, DataHelper.getASCII("b"));
session.sendMessage(d, DataHelper.getASCII("c"));
session.sendMessage(d, DataHelper.getASCII("d"));
for(int i = 0; (i < 20)&&(!_s.isEmpty()); i++){
Thread.sleep(1000);

View File

@ -86,7 +86,7 @@ public class AES256Bench {
System.out.println("Standard test D(E(value)) == value? " + same);
if (!same) throw new RuntimeException("moo");
plain = "1234567890123456".getBytes();
plain = DataHelper.getASCII("1234567890123456");
e = new byte[plain.length];
_context.aes().encrypt(plain, 0, e, 0, key, iv, plain.length);
d = new byte[e.length];

View File

@ -287,9 +287,9 @@ public class AESInputStream extends FilterInputStream {
log.setMinimumPriority(Log.DEBUG);
byte orig[] = new byte[1024 * 32];
RandomSource.getInstance().nextBytes(orig);
//byte orig[] = "you are my sunshine, my only sunshine".getBytes();
//byte orig[] = DataHelper.getASCII("you are my sunshine, my only sunshine");
SessionKey key = KeyGenerator.getInstance().generateSessionKey();
byte iv[] = "there once was a".getBytes();
byte iv[] = DataHelper.getASCII("there once was a");
for (int i = 0; i < 20; i++) {
runTest(ctx, orig, key, iv);

View File

@ -23,7 +23,7 @@ import net.i2p.util.RandomSource;
public class AESInputStreamTest extends TestCase {
public void testMultiple() throws Exception{
SessionKey key = KeyGenerator.getInstance().generateSessionKey();
byte iv[] = "there once was a".getBytes();
byte iv[] = DataHelper.getASCII("there once was a");
int[] sizes = {1024 * 32, 20, 3, 0};
@ -70,7 +70,7 @@ public class AESInputStreamTest extends TestCase {
RandomSource.getInstance().nextBytes(orig);
SessionKey key = KeyGenerator.getInstance().generateSessionKey();
byte iv[] = "there once was a".getBytes();
byte iv[] = DataHelper.getASCII("there once was a");
ByteArrayOutputStream origStream = new ByteArrayOutputStream(512);
AESOutputStream out = new AESOutputStream(ctx, origStream, key, iv);

View File

@ -136,11 +136,11 @@ public class ElGamalTest extends TestCase{
System.arraycopy(h.getData(), 0, iv, 0, 16);
String msg = "Hello world01234012345678901234501234567890123450123456789012345";
h = SHA256Generator.getInstance().calculateHash(msg.getBytes());
h = SHA256Generator.getInstance().calculateHash(DataHelper.getASCII(msg));
byte aesEncr[] = new byte[msg.getBytes().length];
byte aesEncr[] = new byte[DataHelper.getASCII(msg).length];
byte aesDecr[] = new byte[aesEncr.length];
_context.aes().encrypt(msg.getBytes(), 0, aesEncr, 0, sessionKey, iv, aesEncr.length);
_context.aes().encrypt(DataHelper.getASCII(msg), 0, aesEncr, 0, sessionKey, iv, aesEncr.length);
_context.aes().decrypt(aesEncr, 0, aesDecr, 0, sessionKey, iv, aesEncr.length);
h = SHA256Generator.getInstance().calculateHash(aesDecr);
@ -155,7 +155,7 @@ public class ElGamalTest extends TestCase{
String msg = "Hello world";
byte encrypted[] = _context.elGamalAESEngine().encryptAESBlock(msg.getBytes(), sessionKey, iv, null, null, 64);
byte encrypted[] = _context.elGamalAESEngine().encryptAESBlock(DataHelper.getASCII(msg), sessionKey, iv, null, null, 64);
Set<SessionTag> foundTags = new HashSet<SessionTag>();
SessionKey foundKey = new SessionKey();
byte decrypted[] = null;
@ -180,7 +180,7 @@ public class ElGamalTest extends TestCase{
SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
if (key == null)
key = _context.sessionKeyManager().createSession(pubKey);
byte[] encrypted = _context.elGamalAESEngine().encrypt(msg.getBytes(), pubKey, key, 64);
byte[] encrypted = _context.elGamalAESEngine().encrypt(DataHelper.getASCII(msg), pubKey, key, 64);
byte[] decrypted = null;
try{
decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey);
@ -286,7 +286,7 @@ public class ElGamalTest extends TestCase{
PrivateKey priv = (PrivateKey)keypair[1];
for (int i = 0; i < UNENCRYPTED.length; i++) {
byte orig[] = UNENCRYPTED[i].getBytes();
byte orig[] = DataHelper.getASCII(UNENCRYPTED[i]);
byte encrypted[] = _context.elGamalEngine().encrypt(orig, pub);
byte decrypted[] = _context.elGamalEngine().decrypt(encrypted, priv);
@ -310,7 +310,7 @@ public class ElGamalTest extends TestCase{
byte enc[] = Base64.decode(ENCRYPTED[i]);
byte decrypted[] = _context.elGamalEngine().decrypt(enc, priv);
assertTrue(DataHelper.eq(decrypted, UNENCRYPTED[i].getBytes()));
assertTrue(DataHelper.eq(decrypted, DataHelper.getASCII(UNENCRYPTED[i])));
}
}
@ -343,7 +343,7 @@ public class ElGamalTest extends TestCase{
for (int j = 0; j < 5; j++)
tags.add(new SessionTag(true));
}
byte encrypted[] = e.encrypt("blah".getBytes(), pubKey, sessionKey, tags, 1024);
byte encrypted[] = e.encrypt(DataHelper.getASCII("blah"), pubKey, sessionKey, tags, 1024);
byte decrypted[] = e.decrypt(encrypted, privKey);
assertEquals("blah", new String(decrypted));

View File

@ -32,6 +32,7 @@ package net.i2p.crypto;
import java.util.Properties;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
import net.i2p.data.SessionKey;
@ -74,12 +75,12 @@ public class HMACSHA256Bench {
for (int x = 0; x < 2*1024; x++) {
buf.append("a");
}
byte[] mmess = buf.toString().getBytes(); // new String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq").getBytes();
byte[] mmess = DataHelper.getASCII(buf.toString()); // new String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq").getBytes();
buf = new StringBuilder();
for (int x = 0; x < 10000; x++) {
buf.append("a");
}
byte[] lmess = buf.toString().getBytes();
byte[] lmess = DataHelper.getASCII(buf.toString());
// warm up the engines
ctx.hmac().calculate(key, smess);

View File

@ -29,11 +29,12 @@ package net.i2p.crypto;
* POSSIBILITY OF SUCH DAMAGE.
*/
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
public class SHA256Bench {
public static void main(String args[]) {
Hash asdfs = SHA256Generator.getInstance().calculateHash("qwerty".getBytes());
Hash asdfs = SHA256Generator.getInstance().calculateHash(DataHelper.getASCII("qwerty"));
int times = 100;
long shorttime = 0;
@ -56,17 +57,17 @@ public class SHA256Bench {
long minLong1 = 0;
long maxLong1 = 0;
byte[] smess = new String("abc").getBytes();
byte[] smess = DataHelper.getASCII(new String("abc"));
StringBuilder buf = new StringBuilder();
for (int x = 0; x < 10*1024; x++) {
buf.append("a");
}
byte[] mmess = buf.toString().getBytes(); // new String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq").getBytes();
byte[] mmess = DataHelper.getASCII(buf.toString()); // new String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq").getBytes();
buf = new StringBuilder();
for (int x = 0; x < 1000000; x++) {
buf.append("a");
}
byte[] lmess = buf.toString().getBytes();
byte[] lmess = DataHelper.getASCII(buf.toString());
// warm up the engines
SHA256Generator.getInstance().calculateHash(smess);

View File

@ -10,6 +10,7 @@ package net.i2p.crypto;
import junit.framework.TestCase;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.data.Hash;
/**
* @author Comwiz
@ -36,7 +37,7 @@ public class SHA256Test extends TestCase{
* Check if the behaviour remains the same.
*/
public void testMultipleEquality(){
byte[] data = "blahblah".getBytes();
byte[] data = DataHelper.getASCII("blahblah");
Hash firstHash = SHA256Generator.getInstance().calculateHash(data);

View File

@ -13,7 +13,7 @@ import junit.framework.TestCase;
public class Base64Test extends TestCase{
public void testBase64(){
String orig = "you smell";
String encoded = Base64.encode(orig.getBytes());
String encoded = Base64.encode(DataHelper.getASCII(orig));
byte decoded[] = Base64.decode(encoded);
String transformed = new String(decoded);
assertTrue(orig.equals(transformed));

View File

@ -60,7 +60,7 @@ public class DataStructureImplTest extends TestCase{
public void testErrors() throws Exception{
boolean error = false;
try{
_struct.fromByteArray("water is poison".getBytes());
_struct.fromByteArray(DataHelper.getASCII("water is poison"));
}catch(DataFormatException dfe){
error = true;
}

View File

@ -20,7 +20,7 @@ package net.i2p.data;
Payload payload = new Payload();
SessionKey key = (SessionKey)(new SessionKeyTest()).createDataStructure();
byte data[] = "Hello, I2P".getBytes();
byte data[] = DataHelper.getASCII("Hello, I2P");
// This causes equals() to fail unless we override the test
// to set the unencrypted data after reading.
// Unencrypted data is deprecated, just use encrypted data for the test.

View File

@ -81,7 +81,7 @@ public class PrivateKeyTest extends StructureTest {
public void testShortRead() throws Exception{
PrivateKey privateKey = new PrivateKey();
ByteArrayInputStream in = new ByteArrayInputStream("six times nine equals forty-two".getBytes());
ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two"));
boolean error = false;
try{
privateKey.readBytes(in);

View File

@ -81,7 +81,7 @@ public class PublicKeyTest extends StructureTest {
public void testShortRead() throws Exception{
PublicKey publicKey = new PublicKey();
ByteArrayInputStream in = new ByteArrayInputStream("six times nine equals forty-two".getBytes());
ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two"));
boolean error = false;
try{
publicKey.readBytes(in);

View File

@ -81,7 +81,7 @@ public class SigningPrivateKeyTest extends StructureTest {
public void testShortRead() throws Exception{
SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
ByteArrayInputStream in = new ByteArrayInputStream("short".getBytes());
ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("short"));
boolean error = false;
try{
signingPrivateKey.readBytes(in);

View File

@ -82,7 +82,7 @@ public class SigningPublicKeyTest extends StructureTest {
public void testShortRead() throws Exception{
SigningPublicKey publicKey = new SigningPublicKey();
ByteArrayInputStream in = new ByteArrayInputStream("six times nine equals forty-two".getBytes());
ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two"));
boolean error = false;
try{
publicKey.readBytes(in);

View File

@ -2,6 +2,7 @@ package net.i2p.stat;
import java.io.ByteArrayInputStream;
import java.util.Properties;
import net.i2p.data.DataHelper;
import org.junit.Test;
@ -21,7 +22,7 @@ public class RateTest extends TestCase {
StringBuilder buf = new StringBuilder(1024);
rate.store("rate.test", buf);
byte data[] = buf.toString().getBytes();
byte data[] = DataHelper.getUTF8(buf.toString());
Properties props = new Properties();
props.load(new ByteArrayInputStream(data));

View File

@ -45,7 +45,7 @@ public class ResettableGZIPInputStreamTest extends TestCase {
}
byte orig[] = "ho ho ho, merry christmas".getBytes();
byte orig[] = DataHelper.getASCII("ho ho ho, merry christmas");
ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
GZIPOutputStream o = new GZIPOutputStream(baos);
o.write(orig);

View File

@ -18,7 +18,7 @@ import net.i2p.data.DataHelper;
public class ResettableGZIPOutputStreamTest extends TestCase {
public void testResettableGZIPOutputStream() throws Exception{
byte b[] = "hi, how are you today?".getBytes();
byte b[] = DataHelper.getASCII("hi, how are you today?");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ResettableGZIPOutputStream o = new ResettableGZIPOutputStream(baos);
o.write(b);

View File

@ -20,7 +20,7 @@ import net.i2p.data.DataHelper;
public class ReusableGZIPInputStreamTest extends TestCase {
public void testReusableGZIPInputStream() throws Exception{
{
byte b[] = "hi, how are you today?".getBytes();
byte b[] = DataHelper.getASCII("hi, how are you today?");
ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
GZIPOutputStream o = new GZIPOutputStream(baos);
o.write(b);

View File

@ -19,7 +19,7 @@ import net.i2p.data.DataHelper;
public class ReusableGZIPOutputStreamTest extends TestCase {
public void testReusableGZIPOutputStream() throws Exception{
{
byte b[] = "hi, how are you today?".getBytes();
byte b[] = DataHelper.getASCII("hi, how are you today?");
ReusableGZIPOutputStream o = ReusableGZIPOutputStream.acquire();
o.write(b);
o.finish();