本文整理汇总了Java中com.trilead.ssh2.crypto.Base64类的典型用法代码示例。如果您正苦于以下问题:Java Base64类的具体用法?Java Base64怎么用?Java Base64使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Base64类属于com.trilead.ssh2.crypto包,在下文中一共展示了Base64类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createHashedHostname
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
/**
* Generate the hashed representation of the given hostname. Useful for adding entries
* with hashed hostnames to a known_hosts file. (see -H option of OpenSSH key-gen).
*
* @param hostname
* @return the hashed representation, e.g., "|1|cDhrv7zwEUV3k71CEPHnhHZezhA=|Xo+2y6rUXo2OIWRAYhBOIijbJMA="
*/
public static final String createHashedHostname(String hostname)
{
MessageDigest sha1;
try {
sha1 = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("VM doesn't support SHA1", e);
}
byte[] salt = new byte[sha1.getDigestLength()];
new SecureRandom().nextBytes(salt);
byte[] hash = hmacSha1Hash(salt, hostname);
String base64_salt = new String(Base64.encode(salt));
String base64_hash = new String(Base64.encode(hash));
return new String("|1|" + base64_salt + "|" + base64_hash);
}
示例2: writeLogTo
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
@Override
public long writeLogTo(long start, int size, Writer w) throws IOException {
if (isHtml()) {
ConsoleAnnotationOutputStream caw = new ConsoleAnnotationOutputStream(w, this.createAnnotator(Stapler.getCurrentRequest()), this.context, this.charset);
long r = super.writeLogTo(start, size, caw);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Cipher sym = PASSING_ANNOTATOR.encrypt();
ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos, sym)));
oos.writeLong(System.currentTimeMillis());
oos.writeObject(caw.getConsoleAnnotator());
oos.close();
StaplerResponse rsp = Stapler.getCurrentResponse();
if(rsp != null) {
rsp.setHeader("X-ConsoleAnnotator", new String(Base64.encode(baos.toByteArray())));
}
return r;
} else {
return super.writeLogTo(start, size, w);
}
}
示例3: importPEM
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
public static KeyPair importPEM (String pem) {
byte [] encoded = null;
KeyPair recovered = null;
// Get rid of any header and footer.
String privKeyPEM = pem.replace("-----BEGIN RSA PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", "");
privKeyPEM = privKeyPEM.replace("-----BEGIN DSA PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END DSA PRIVATE KEY-----", "");
privKeyPEM = privKeyPEM.replace("-----BEGIN PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
try {
encoded = android.util.Base64.decode(privKeyPEM, android.util.Base64.DEFAULT);
recovered = recoverKeyPair(encoded);
} catch (Exception e) {
Log.e (TAG, "Could not recover keypair from PEM string.");
e.printStackTrace();
return null;
}
return recovered;
}
示例4: importPkcs8
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
public static KeyPair importPkcs8 (String pem) throws Exception {
byte [] encoded = null;
KeyPair recovered = null;
// Get rid of any header and footer.
String privKeyPEM = pem.replace(PKCS8_START + "\n", "");
privKeyPEM = privKeyPEM.replace(PKCS8_END, "");
try {
encoded = android.util.Base64.decode(privKeyPEM, android.util.Base64.DEFAULT);
recovered = recoverKeyPair(encoded);
} catch (Exception e) {
Log.e (TAG, "Could not recover keypair from PKCS8 string.");
e.printStackTrace();
}
return recovered;
}
示例5: readPKCS8Key
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
private KeyPair readPKCS8Key(byte[] keyData) {
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(keyData)));
// parse the actual key once to check if its encrypted
// then save original file contents into our database
try {
ByteArrayOutputStream keyBytes = new ByteArrayOutputStream();
String line;
boolean inKey = false;
while ((line = reader.readLine()) != null) {
if (line.equals(PubkeyUtils.PKCS8_START)) {
inKey = true;
} else if (line.equals(PubkeyUtils.PKCS8_END)) {
break;
} else if (inKey) {
keyBytes.write(line.getBytes("US-ASCII"));
}
}
if (keyBytes.size() > 0) {
byte[] decoded = Base64.decode(keyBytes.toString().toCharArray());
return PubkeyUtils.recoverKeyPair(decoded);
}
} catch (Exception e) {
return null;
}
return null;
}
示例6: append
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
public XmlBuilder append(String field, Object data) {
if (data == null) {
sb.append(String.format("<%s/>", field));
} else if (data instanceof String) {
String input = (String) data;
boolean binary = false;
for (byte b : input.getBytes()) {
if (b < 0x20 || b > 0x7e) {
binary = true;
break;
}
}
sb.append(String.format("<%s>%s</%s>", field,
binary ? new String(Base64.encode(input.getBytes())) : input, field));
} else if (data instanceof Integer) {
sb.append(String.format("<%s>%d</%s>", field, (Integer) data, field));
} else if (data instanceof Long) {
sb.append(String.format("<%s>%d</%s>", field, (Long) data, field));
} else if (data instanceof byte[]) {
sb.append(String.format("<%s>%s</%s>", field, new String(Base64.encode((byte[]) data)), field));
} else if (data instanceof Boolean) {
sb.append(String.format("<%s>%s</%s>", field, (Boolean) data, field));
}
return this;
}
示例7: initialize
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
private void initialize(char[] knownHostsData) throws IOException
{
BufferedReader br = new BufferedReader(new CharArrayReader(knownHostsData));
while (true)
{
String line = br.readLine();
if (line == null)
break;
line = line.trim();
if (line.startsWith("#"))
continue;
String[] arr = line.split(" ");
if (arr.length >= 3)
{
if ((arr[1].compareTo("ssh-rsa") == 0) || (arr[1].compareTo("ssh-dss") == 0))
{
String[] hostnames = arr[0].split(",");
byte[] msg = Base64.decode(arr[2].toCharArray());
addHostkey(hostnames, arr[1], msg);
}
}
}
}
示例8: checkHashed
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
private final boolean checkHashed(String entry, String hostname)
{
if (entry.startsWith("|1|") == false)
return false;
int delim_idx = entry.indexOf('|', 3);
if (delim_idx == -1)
return false;
String salt_base64 = entry.substring(3, delim_idx);
String hash_base64 = entry.substring(delim_idx + 1);
byte[] salt = null;
byte[] hash = null;
try
{
salt = Base64.decode(salt_base64.toCharArray());
hash = Base64.decode(hash_base64.toCharArray());
}
catch (IOException e)
{
return false;
}
SHA1 sha1 = new SHA1();
if (salt.length != sha1.getDigestLength())
return false;
byte[] dig = hmacSha1Hash(salt, hostname);
for (int i = 0; i < dig.length; i++)
if (dig[i] != hash[i])
return false;
return true;
}
示例9: checkHashed
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
private final boolean checkHashed(String entry, String hostname) {
if (entry.startsWith("|1|") == false)
return false;
int delim_idx = entry.indexOf('|', 3);
if (delim_idx == -1)
return false;
String salt_base64 = entry.substring(3, delim_idx);
String hash_base64 = entry.substring(delim_idx + 1);
byte[] salt = null;
byte[] hash = null;
try {
salt = Base64.decode(salt_base64.toCharArray());
hash = Base64.decode(hash_base64.toCharArray());
} catch (IOException e) {
return false;
}
SHA1 sha1 = new SHA1();
if (salt.length != sha1.getDigestLength())
return false;
byte[] dig = hmacSha1Hash(salt, hostname);
for (int i = 0; i < dig.length; i++)
if (dig[i] != hash[i])
return false;
return true;
}
示例10: initialize
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
private void initialize(char[] knownHostsData) throws IOException {
BufferedReader br = new BufferedReader(new CharArrayReader(
knownHostsData));
while (true) {
String line = br.readLine();
if (line == null)
break;
line = line.trim();
if (line.startsWith("#"))
continue;
String[] arr = line.split(" ");
if (arr.length >= 3) {
if ((arr[1].compareTo("ssh-rsa") == 0)
|| (arr[1].compareTo("ssh-dss") == 0)) {
String[] hostnames = arr[0].split(",");
byte[] msg = Base64.decode(arr[2].toCharArray());
addHostkey(hostnames, arr[1], msg);
}
}
}
}
示例11: writeHtmlTo
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
public long writeHtmlTo(long start, Writer w) throws IOException {
ConsoleAnnotationOutputStream caw = new ConsoleAnnotationOutputStream(w, this.createAnnotator(Stapler.getCurrentRequest()), this.context, this.charset);
long r = super.writeLogTo(start, caw);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Cipher sym = PASSING_ANNOTATOR.encrypt();
ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos, sym)));
oos.writeLong(System.currentTimeMillis());
oos.writeObject(caw.getConsoleAnnotator());
oos.close();
StaplerResponse rsp = Stapler.getCurrentResponse();
if(rsp != null) {
rsp.setHeader("X-ConsoleAnnotator", new String(Base64.encode(baos.toByteArray())));
}
return r;
}
示例12: exportPEM
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
public static String exportPEM(PrivateKey key, String secret) throws NoSuchAlgorithmException, InvalidParameterSpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException, IllegalBlockSizeException, IOException {
StringBuilder sb = new StringBuilder();
byte[] data = key.getEncoded();
sb.append(PKCS8_START);
sb.append('\n');
if (secret != null) {
byte[] salt = new byte[8];
SecureRandom random = new SecureRandom();
random.nextBytes(salt);
PBEParameterSpec defParams = new PBEParameterSpec(salt, 1);
AlgorithmParameters params = AlgorithmParameters.getInstance(key.getAlgorithm());
params.init(defParams);
PBEKeySpec pbeSpec = new PBEKeySpec(secret.toCharArray());
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(key.getAlgorithm());
Cipher cipher = Cipher.getInstance(key.getAlgorithm());
cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), params);
byte[] wrappedKey = cipher.wrap(key);
EncryptedPrivateKeyInfo pinfo = new EncryptedPrivateKeyInfo(params, wrappedKey);
data = pinfo.getEncoded();
sb.append("Proc-Type: 4,ENCRYPTED\n");
sb.append("DEK-Info: DES-EDE3-CBC,");
sb.append(encodeHex(salt));
sb.append("\n\n");
}
int i = sb.length();
sb.append(Base64.encode(data));
for (i += 63; i < sb.length(); i += 64) {
sb.insert(i, "\n");
}
sb.append('\n');
sb.append(PKCS8_END);
sb.append('\n');
return sb.toString();
}
示例13: addHostkeyToFile
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
/**
* Adds a single public key entry to the a known_hosts file.
* This method is designed to be used in a {@link ServerHostKeyVerifier}.
*
* @param knownHosts the file where the publickey entry will be appended.
* @param hostnames a list of hostname patterns - at least one most be specified. Check out the
* OpenSSH sshd man page for a description of the pattern matching algorithm.
* @param serverHostKeyAlgorithm as passed to the {@link ServerHostKeyVerifier}.
* @param serverHostKey as passed to the {@link ServerHostKeyVerifier}.
* @throws IOException
*/
public final static void addHostkeyToFile(File knownHosts, String[] hostnames, String serverHostKeyAlgorithm,
byte[] serverHostKey) throws IOException
{
if ((hostnames == null) || (hostnames.length == 0))
throw new IllegalArgumentException("Need at least one hostname specification");
if ((serverHostKeyAlgorithm == null) || (serverHostKey == null))
throw new IllegalArgumentException();
CharArrayWriter writer = new CharArrayWriter();
for (int i = 0; i < hostnames.length; i++)
{
if (i != 0)
writer.write(',');
writer.write(hostnames[i]);
}
writer.write(' ');
writer.write(serverHostKeyAlgorithm);
writer.write(' ');
writer.write(Base64.encode(serverHostKey));
writer.write("\n");
char[] entry = writer.toCharArray();
RandomAccessFile raf = new RandomAccessFile(knownHosts, "rw");
long len = raf.length();
if (len > 0)
{
raf.seek(len - 1);
int last = raf.read();
if (last != '\n')
raf.write('\n');
}
raf.write(new String(entry).getBytes("ISO-8859-1"));
raf.close();
}
示例14: createHashedHostname
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
/**
* Generate the hashed representation of the given hostname. Useful for adding entries
* with hashed hostnames to a known_hosts file. (see -H option of OpenSSH key-gen).
*
* @param hostname
* @return the hashed representation, e.g., "|1|cDhrv7zwEUV3k71CEPHnhHZezhA=|Xo+2y6rUXo2OIWRAYhBOIijbJMA="
*/
public static final String createHashedHostname(String hostname)
{
SHA1 sha1 = new SHA1();
byte[] salt = new byte[sha1.getDigestLength()];
new SecureRandom().nextBytes(salt);
byte[] hash = hmacSha1Hash(salt, hostname);
String base64_salt = new String(Base64.encode(salt));
String base64_hash = new String(Base64.encode(hash));
return new String("|1|" + base64_salt + "|" + base64_hash);
}
示例15: addHostkeyToFile
import com.trilead.ssh2.crypto.Base64; //导入依赖的package包/类
/**
* Adds a single public key entry to the a known_hosts file. This method is
* designed to be used in a {@link ServerHostKeyVerifier}.
*
* @param knownHosts
* the file where the publickey entry will be appended.
* @param hostnames
* a list of hostname patterns - at least one most be specified.
* Check out the OpenSSH sshd man page for a description of the
* pattern matching algorithm.
* @param serverHostKeyAlgorithm
* as passed to the {@link ServerHostKeyVerifier}.
* @param serverHostKey
* as passed to the {@link ServerHostKeyVerifier}.
* @throws IOException
*/
public final static void addHostkeyToFile(File knownHosts,
String[] hostnames, String serverHostKeyAlgorithm,
byte[] serverHostKey) throws IOException {
if ((hostnames == null) || (hostnames.length == 0))
throw new IllegalArgumentException(
"Need at least one hostname specification");
if ((serverHostKeyAlgorithm == null) || (serverHostKey == null))
throw new IllegalArgumentException();
CharArrayWriter writer = new CharArrayWriter();
for (int i = 0; i < hostnames.length; i++) {
if (i != 0)
writer.write(',');
writer.write(hostnames[i]);
}
writer.write(' ');
writer.write(serverHostKeyAlgorithm);
writer.write(' ');
writer.write(Base64.encode(serverHostKey));
writer.write("\n");
char[] entry = writer.toCharArray();
RandomAccessFile raf = new RandomAccessFile(knownHosts, "rw");
long len = raf.length();
if (len > 0) {
raf.seek(len - 1);
int last = raf.read();
if (last != '\n')
raf.write('\n');
}
raf.write(new String(entry).getBytes("ISO-8859-1"));
raf.close();
}