Use StandardCharsets.UTF_8 (Java 1.7) (#998)
Some checks are pending
Build SSHJ / Build with Java 11 (push) Waiting to run
Build SSHJ / Integration test (push) Waiting to run

Co-authored-by: Jeroen van Erp <jeroen@hierynomus.com>
This commit is contained in:
Simon Legner
2025-03-18 21:44:31 +01:00
committed by GitHub
parent b886085da5
commit 0816bf95af
15 changed files with 31 additions and 26 deletions

View File

@@ -17,11 +17,11 @@ package com.hierynomus.sshj.transport.verification;
import net.schmizz.sshj.common.Base64DecodingException; import net.schmizz.sshj.common.Base64DecodingException;
import net.schmizz.sshj.common.Base64Decoder; import net.schmizz.sshj.common.Base64Decoder;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.SSHException; import net.schmizz.sshj.common.SSHException;
import net.schmizz.sshj.transport.mac.MAC; import net.schmizz.sshj.transport.mac.MAC;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
@@ -96,7 +96,7 @@ public class KnownHostMatchers {
private String hashHost(String host) throws IOException, Base64DecodingException { private String hashHost(String host) throws IOException, Base64DecodingException {
sha1.init(getSaltyBytes()); sha1.init(getSaltyBytes());
return "|1|" + salt + "|" + Base64.getEncoder().encodeToString(sha1.doFinal(host.getBytes(IOUtils.UTF8))); return "|1|" + salt + "|" + Base64.getEncoder().encodeToString(sha1.doFinal(host.getBytes(StandardCharsets.UTF_8)));
} }
private byte[] getSaltyBytes() throws IOException, Base64DecodingException { private byte[] getSaltyBytes() throws IOException, Base64DecodingException {

View File

@@ -59,6 +59,7 @@ import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair; import java.security.KeyPair;
import java.util.*; import java.util.*;
@@ -126,7 +127,7 @@ public class SSHClient
private final List<LocalPortForwarder> forwarders = new ArrayList<LocalPortForwarder>(); private final List<LocalPortForwarder> forwarders = new ArrayList<LocalPortForwarder>();
/** character set of the remote machine */ /** character set of the remote machine */
protected Charset remoteCharset = IOUtils.UTF8; protected Charset remoteCharset = StandardCharsets.UTF_8;
/** Default constructor. Initializes this object using {@link DefaultConfig}. */ /** Default constructor. Initializes this object using {@link DefaultConfig}. */
public SSHClient() { public SSHClient() {
@@ -765,7 +766,7 @@ public class SSHClient
* remote character set or {@code null} for default * remote character set or {@code null} for default
*/ */
public void setRemoteCharset(Charset remoteCharset) { public void setRemoteCharset(Charset remoteCharset) {
this.remoteCharset = remoteCharset != null ? remoteCharset : IOUtils.UTF8; this.remoteCharset = remoteCharset != null ? remoteCharset : StandardCharsets.UTF_8;
} }
@Override @Override

View File

@@ -17,6 +17,7 @@ package net.schmizz.sshj.common;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.PublicKey; import java.security.PublicKey;
import java.util.Arrays; import java.util.Arrays;
@@ -428,7 +429,7 @@ public class Buffer<T extends Buffer<T>> {
*/ */
public String readString() public String readString()
throws BufferException { throws BufferException {
return readString(IOUtils.UTF8); return readString(StandardCharsets.UTF_8);
} }
/** /**
@@ -454,7 +455,7 @@ public class Buffer<T extends Buffer<T>> {
} }
public T putString(String string) { public T putString(String string) {
return putString(string, IOUtils.UTF8); return putString(string, StandardCharsets.UTF_8);
} }
/** /**

View File

@@ -17,8 +17,8 @@ package net.schmizz.sshj.common;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder; import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
/** Utility functions for byte arrays. */ /** Utility functions for byte arrays. */
@@ -141,7 +141,7 @@ public class ByteArrayUtils {
* @return UTF-8 bytes of the string * @return UTF-8 bytes of the string
*/ */
public static byte[] encodeSensitiveStringToUtf8(char[] str) { public static byte[] encodeSensitiveStringToUtf8(char[] str) {
CharsetEncoder charsetEncoder = Charset.forName("UTF-8").newEncoder(); CharsetEncoder charsetEncoder = StandardCharsets.UTF_8.newEncoder();
ByteBuffer utf8Buffer = ByteBuffer.allocate((int) (str.length * charsetEncoder.maxBytesPerChar())); ByteBuffer utf8Buffer = ByteBuffer.allocate((int) (str.length * charsetEncoder.maxBytesPerChar()));
assert utf8Buffer.hasArray(); assert utf8Buffer.hasArray();
charsetEncoder.encode(CharBuffer.wrap(str), utf8Buffer, true); charsetEncoder.encode(CharBuffer.wrap(str), utf8Buffer, true);

View File

@@ -19,12 +19,9 @@ import java.io.ByteArrayOutputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset;
public class IOUtils { public class IOUtils {
public static final Charset UTF8 = Charset.forName("UTF-8");
public static void closeQuietly(Closeable... closeables) { public static void closeQuietly(Closeable... closeables) {
closeQuietly(LoggerFactory.DEFAULT, closeables); closeQuietly(LoggerFactory.DEFAULT, closeables);
} }

View File

@@ -27,6 +27,7 @@ import org.slf4j.Logger;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -90,7 +91,7 @@ public abstract class AbstractChannel
this.log = loggerFactory.getLogger(getClass()); this.log = loggerFactory.getLogger(getClass());
this.trans = conn.getTransport(); this.trans = conn.getTransport();
this.remoteCharset = remoteCharset != null ? remoteCharset : IOUtils.UTF8; this.remoteCharset = remoteCharset != null ? remoteCharset : StandardCharsets.UTF_8;
id = conn.nextID(); id = conn.nextID();
lwin = new Window.Local(conn.getWindowSize(), conn.getMaxPacketSize(), loggerFactory); lwin = new Window.Local(conn.getWindowSize(), conn.getMaxPacketSize(), loggerFactory);

View File

@@ -17,7 +17,6 @@ package net.schmizz.sshj.sftp;
import com.hierynomus.sshj.common.ThreadNameProvider; import com.hierynomus.sshj.common.ThreadNameProvider;
import net.schmizz.concurrent.Promise; import net.schmizz.concurrent.Promise;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.LoggerFactory; import net.schmizz.sshj.common.LoggerFactory;
import net.schmizz.sshj.common.SSHException; import net.schmizz.sshj.common.SSHException;
import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.connection.channel.direct.Session;
@@ -28,6 +27,7 @@ import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -373,7 +373,7 @@ public class SFTPEngine
/** Using UTF-8 */ /** Using UTF-8 */
protected static String readSingleName(Response res) protected static String readSingleName(Response res)
throws IOException { throws IOException {
return readSingleName(res, IOUtils.UTF8); return readSingleName(res, StandardCharsets.UTF_8);
} }
/** Using any character set */ /** Using any character set */

View File

@@ -33,6 +33,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -207,7 +208,7 @@ public final class TransportImpl
*/ */
private void sendClientIdent() throws IOException { private void sendClientIdent() throws IOException {
log.info("Client identity string: {}", clientID); log.info("Client identity string: {}", clientID);
connInfo.out.write((clientID + "\r\n").getBytes(IOUtils.UTF8)); connInfo.out.write((clientID + "\r\n").getBytes(StandardCharsets.UTF_8));
connInfo.out.flush(); connInfo.out.flush();
} }

View File

@@ -31,6 +31,7 @@ import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory; import java.security.KeyFactory;
import java.security.PublicKey; import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPublicKeySpec;
@@ -189,7 +190,7 @@ public class OpenSSHKnownHosts
final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(khFile)); final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(khFile));
try { try {
for (KnownHostEntry entry : entries) for (KnownHostEntry entry : entries)
bos.write((entry.getLine() + LS).getBytes(IOUtils.UTF8)); bos.write((entry.getLine() + LS).getBytes(StandardCharsets.UTF_8));
} finally { } finally {
bos.close(); bos.close();
} }

View File

@@ -16,6 +16,7 @@
package net.schmizz.sshj.userauth.password; package net.schmizz.sshj.userauth.password;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
public class PrivateKeyFileResource public class PrivateKeyFileResource
extends Resource<File> { extends Resource<File> {
@@ -27,6 +28,6 @@ public class PrivateKeyFileResource
@Override @Override
public Reader getReader() public Reader getReader()
throws IOException { throws IOException {
return new InputStreamReader(new FileInputStream(getDetail()), "UTF-8"); return new InputStreamReader(new FileInputStream(getDetail()), StandardCharsets.UTF_8);
} }
} }

View File

@@ -29,6 +29,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
/** @see <a href="https://blogs.oracle.com/janp/entry/how_the_scp_protocol_works">SCP Protocol</a> */ /** @see <a href="https://blogs.oracle.com/janp/entry/how_the_scp_protocol_works">SCP Protocol</a> */
class SCPEngine { class SCPEngine {
@@ -128,7 +129,7 @@ class SCPEngine {
baos.write(x); baos.write(x);
} }
} }
final String msg = baos.toString(IOUtils.UTF8.displayName()); final String msg = baos.toString(StandardCharsets.UTF_8.displayName());
log.debug("Read message: `{}`", msg); log.debug("Read message: `{}`", msg);
return msg; return msg;
} }

View File

@@ -18,6 +18,7 @@ package com.hierynomus.sshj.test.util;
import net.schmizz.sshj.common.IOUtils; import net.schmizz.sshj.common.IOUtils;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
public class FileUtil { public class FileUtil {
@@ -34,12 +35,12 @@ public class FileUtil {
FileInputStream fileInputStream = new FileInputStream(f); FileInputStream fileInputStream = new FileInputStream(f);
try { try {
ByteArrayOutputStream byteArrayOutputStream = IOUtils.readFully(fileInputStream); ByteArrayOutputStream byteArrayOutputStream = IOUtils.readFully(fileInputStream);
return byteArrayOutputStream.toString(IOUtils.UTF8.displayName()); return byteArrayOutputStream.toString(StandardCharsets.UTF_8.displayName());
} finally { } finally {
IOUtils.closeQuietly(fileInputStream); IOUtils.closeQuietly(fileInputStream);
} }
} }
public static boolean compareFileContents(File f1, File f2) throws IOException { public static boolean compareFileContents(File f1, File f2) throws IOException {
return readFromFile(f1).equals(readFromFile(f2)); return readFromFile(f1).equals(readFromFile(f2));
} }

View File

@@ -17,11 +17,11 @@ package net.schmizz.sshj.signature;
import com.hierynomus.sshj.common.KeyAlgorithm; import com.hierynomus.sshj.common.KeyAlgorithm;
import net.schmizz.sshj.common.Buffer; import net.schmizz.sshj.common.Buffer;
import net.schmizz.sshj.common.IOUtils;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory; import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.spec.DSAPrivateKeySpec; import java.security.spec.DSAPrivateKeySpec;
@@ -47,7 +47,7 @@ public class SignatureDSATest {
BigInteger q = new BigInteger(new byte[] { 0, -105, 96, 80, -113, 21, 35, 11, -52, -78, -110, -71, -126, -94, -21, -124, 11, -16, 88, 28, -11 }); BigInteger q = new BigInteger(new byte[] { 0, -105, 96, 80, -113, 21, 35, 11, -52, -78, -110, -71, -126, -94, -21, -124, 11, -16, 88, 28, -11 });
BigInteger g = new BigInteger(new byte[] { 0, -9, -31, -96, -123, -42, -101, 61, -34, -53, -68, -85, 92, 54, -72, 87, -71, 121, -108, -81, -69, -6, 58, -22, -126, -7, 87, 76, 11, 61, 7, -126, 103, 81, 89, 87, -114, -70, -44, 89, 79, -26, 113, 7, 16, -127, -128, -76, 73, 22, 113, 35, -24, 76, 40, 22, 19, -73, -49, 9, 50, -116, -56, -90, -31, 60, 22, 122, -117, 84, 124, -115, 40, -32, -93, -82, 30, 43, -77, -90, 117, -111, 110, -93, 127, 11, -6, 33, 53, 98, -15, -5, 98, 122, 1, 36, 59, -52, -92, -15, -66, -88, 81, -112, -119, -88, -125, -33, -31, 90, -27, -97, 6, -110, -117, 102, 94, -128, 123, 85, 37, 100, 1, 76, 59, -2, -49, 73, 42 }); BigInteger g = new BigInteger(new byte[] { 0, -9, -31, -96, -123, -42, -101, 61, -34, -53, -68, -85, 92, 54, -72, 87, -71, 121, -108, -81, -69, -6, 58, -22, -126, -7, 87, 76, 11, 61, 7, -126, 103, 81, 89, 87, -114, -70, -44, 89, 79, -26, 113, 7, 16, -127, -128, -76, 73, 22, 113, 35, -24, 76, 40, 22, 19, -73, -49, 9, 50, -116, -56, -90, -31, 60, 22, 122, -117, 84, 124, -115, 40, -32, -93, -82, 30, 43, -77, -90, 117, -111, 110, -93, 127, 11, -6, 33, 53, 98, -15, -5, 98, 122, 1, 36, 59, -52, -92, -15, -66, -88, 81, -112, -119, -88, -125, -33, -31, 90, -27, -97, 6, -110, -117, 102, 94, -128, 123, 85, 37, 100, 1, 76, 59, -2, -49, 73, 42 });
byte[] data = "The Magic Words are Squeamish Ossifrage".getBytes(IOUtils.UTF8); byte[] data = "The Magic Words are Squeamish Ossifrage".getBytes(StandardCharsets.UTF_8);
// A previously signed and verified signature using the data and DSA key parameters above. // A previously signed and verified signature using the data and DSA key parameters above.
byte[] dataSig = new byte[] { 0, 0, 0, 7, 115, 115, 104, 45, 100, 115, 115, 0, 0, 0, 40, 40, -71, 33, 105, -89, -107, 8, 26, -13, -90, 73, -103, 105, 112, 7, -59, -66, 46, 85, -27, 20, 82, 22, -113, -75, -86, -121, -42, -73, 78, 66, 93, -34, 39, -50, -93, 27, -5, 37, -92 }; byte[] dataSig = new byte[] { 0, 0, 0, 7, 115, 115, 104, 45, 100, 115, 115, 0, 0, 0, 40, 40, -71, 33, 105, -89, -107, 8, 26, -13, -90, 73, -103, 105, 112, 7, -59, -66, 46, 85, -27, 20, 82, 22, -113, -75, -86, -121, -42, -73, 78, 66, 93, -34, 39, -50, -93, 27, -5, 37, -92 };

View File

@@ -31,12 +31,12 @@
package net.schmizz.sshj.util; package net.schmizz.sshj.util;
import net.schmizz.sshj.common.Buffer; import net.schmizz.sshj.common.Buffer;
import net.schmizz.sshj.common.IOUtils;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@@ -51,7 +51,7 @@ public class BufferTest {
public void setUp() public void setUp()
throws UnsupportedEncodingException, GeneralSecurityException { throws UnsupportedEncodingException, GeneralSecurityException {
// for position test // for position test
byte[] data = "Hello".getBytes(IOUtils.UTF8); byte[] data = "Hello".getBytes(StandardCharsets.UTF_8);
posBuf = new Buffer.PlainBuffer(data); posBuf = new Buffer.PlainBuffer(data);
handyBuf = new Buffer.PlainBuffer(); handyBuf = new Buffer.PlainBuffer();
} }

View File

@@ -15,13 +15,13 @@
*/ */
package net.schmizz.sshj.util.gss; package net.schmizz.sshj.util.gss;
import net.schmizz.sshj.common.IOUtils;
import org.ietf.jgss.*; import org.ietf.jgss.*;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import static net.schmizz.sshj.util.gss.BogusGSSManager.unavailable; import static net.schmizz.sshj.util.gss.BogusGSSManager.unavailable;
@@ -34,7 +34,7 @@ public class BogusGSSContext
private static final byte[] MIC = fromString("LGTM"); private static final byte[] MIC = fromString("LGTM");
private static byte[] fromString(String s) { private static byte[] fromString(String s) {
return s.getBytes(IOUtils.UTF8); return s.getBytes(StandardCharsets.UTF_8);
} }
private boolean initialized = false; private boolean initialized = false;