Search in sources :

Example 1 with SHA1Hasher

use of com.biglybt.core.util.SHA1Hasher in project BiglyBT by BiglySoftware.

the class LocaleUtilDecoderFallback method decode.

protected String decode(byte[] data) {
    if (data == null) {
        return (null);
    }
    StringBuffer res = new StringBuffer(data.length * 2);
    for (int i = 0; i < data.length; i++) {
        byte c = data[i];
        if (VALID_CHARS.indexOf(Character.toLowerCase((char) c)) != -1) {
            res.append((char) c);
        } else {
            res.append("_");
            res.append(ByteFormatter.nicePrint(c));
        }
    }
    // more often that not these decoded values are used for filenames. Windows has a limit
    // of 250 (ish) chars, so we do something sensible with longer values
    int len = res.length();
    if (len > max_ok_name_length) {
        if ((!max_ok_name_length_determined) && fileLengthOK(len)) {
            // this length is ok, bump up the known limit
            max_ok_name_length = len;
        } else {
            if (!max_ok_name_length_determined) {
                for (int i = max_ok_name_length + 16; i < len; i += 16) {
                    if (fileLengthOK(i)) {
                        max_ok_name_length = i;
                    } else {
                        break;
                    }
                }
                max_ok_name_length_determined = true;
            }
            // try and preserve extension
            String extension = null;
            int pos = res.lastIndexOf(".");
            if (pos != -1) {
                // include the "."
                extension = res.substring(pos);
                if (extension.length() == 1 || extension.length() > 4) {
                    extension = null;
                }
            }
            // replace the end of the string with a hash value to ensure uniqueness
            byte[] hash = new SHA1Hasher().calculateHash(data);
            String hash_str = ByteFormatter.nicePrint(hash, true);
            res = new StringBuffer(res.substring(0, max_ok_name_length - hash_str.length() - (extension == null ? 0 : extension.length())));
            res.append(hash_str);
            if (extension != null) {
                res.append(extension);
            }
        }
    }
    return (res.toString());
}
Also used : SHA1Hasher(com.biglybt.core.util.SHA1Hasher)

Example 2 with SHA1Hasher

use of com.biglybt.core.util.SHA1Hasher in project BiglyBT by BiglySoftware.

the class Set method execute.

@Override
public void execute(String commandName, ConsoleInput ci, List args) {
    boolean non_defaults = false;
    Iterator it = args.iterator();
    while (it.hasNext()) {
        String arg = (String) it.next();
        if (arg.equals("-export")) {
            non_defaults = true;
            it.remove();
        }
    }
    if (args.isEmpty()) {
        displayOptions(ci.out, new StringPattern("*"), non_defaults);
        return;
    }
    String external_name = (String) args.get(0);
    String internal_name = (String) ExternalUIConst.parameterlegacy.get(external_name);
    if (internal_name == null || internal_name.length() == 0) {
        internal_name = external_name;
    }
    // else
    // ci.out.println("> converting " + origParamName + " to " + parameter);
    Parameter param;
    switch(args.size()) {
        case 1:
            // allow wildcards : eg: Core* or *DHT* to shorten result list
            StringPattern sp = new StringPattern(internal_name);
            if (sp.hasWildcard()) {
                displayOptions(ci.out, sp, non_defaults);
            } else {
                // try to display the value of the specified parameter
                if (!COConfigurationManager.doesParameterDefaultExist(internal_name)) {
                    ci.out.println("> Command 'set': Parameter '" + external_name + "' unknown.");
                    return;
                }
                param = Parameter.get(internal_name, external_name);
                ci.out.println(param.getString(false));
            }
            break;
        case 2:
        case 3:
            String setto = (String) args.get(1);
            String type;
            if (args.size() == 2) {
                // guess the parameter type by getting the current value and determining its type
                param = Parameter.get(internal_name, external_name);
                type = param.getType();
            } else
                type = (String) args.get(2);
            boolean success = false;
            if (type.equalsIgnoreCase("int") || type.equalsIgnoreCase("integer")) {
                COConfigurationManager.setParameter(internal_name, Integer.parseInt(setto));
                success = true;
            } else if (type.equalsIgnoreCase("bool") || type.equalsIgnoreCase("boolean")) {
                boolean value;
                if (setto.equalsIgnoreCase("true") || setto.equalsIgnoreCase("y") || setto.equals("1")) {
                    value = true;
                } else {
                    value = false;
                }
                COConfigurationManager.setParameter(internal_name, value);
                success = true;
            } else if (type.equalsIgnoreCase("float")) {
                COConfigurationManager.setParameter(internal_name, Float.parseFloat(setto));
                success = true;
            } else if (type.equalsIgnoreCase("string")) {
                COConfigurationManager.setParameter(internal_name, setto);
                success = true;
            } else if (type.equalsIgnoreCase("password")) {
                SHA1Hasher hasher = new SHA1Hasher();
                byte[] password = setto.getBytes();
                byte[] encoded;
                if (password.length > 0) {
                    encoded = hasher.calculateHash(password);
                } else {
                    encoded = password;
                }
                COConfigurationManager.setParameter(internal_name, encoded);
                success = true;
            }
            if (success) {
                COConfigurationManager.save();
                ci.out.println("> Parameter '" + external_name + "' set to '" + setto + "'. [" + type + "]");
            } else
                ci.out.println("ERROR: invalid type given");
            break;
        default:
            ci.out.println("Usage: 'set \"parameter\" value type', where type = int, bool, float, string, password");
            break;
    }
}
Also used : SHA1Hasher(com.biglybt.core.util.SHA1Hasher) Iterator(java.util.Iterator) StringPattern(com.biglybt.ui.common.util.StringPattern)

Example 3 with SHA1Hasher

use of com.biglybt.core.util.SHA1Hasher in project BiglyBT by BiglySoftware.

the class TOTorrentFileHasher method add.

long add(File _file) throws TOTorrentException {
    long file_length = 0;
    InputStream is = null;
    SHA1Hasher sha1_hash = null;
    ED2KHasher ed2k_hash = null;
    try {
        if (do_other_per_file_hash) {
            sha1_hash = new SHA1Hasher();
            ed2k_hash = new ED2KHasher();
        }
        is = new BufferedInputStream(new FileInputStream(_file), 65536);
        while (true) {
            if (cancelled) {
                throw (new TOTorrentException("TOTorrentCreate: operation cancelled", TOTorrentException.RT_CANCELLED));
            }
            int len = is.read(buffer, buffer_pos, piece_length - buffer_pos);
            if (len > 0) {
                if (do_other_per_file_hash) {
                    sha1_hash.update(buffer, buffer_pos, len);
                    ed2k_hash.update(buffer, buffer_pos, len);
                }
                file_length += len;
                buffer_pos += len;
                if (buffer_pos == piece_length) {
                    // hash this piece
                    byte[] hash = new SHA1Hasher().calculateHash(buffer);
                    if (overall_sha1_hash != null) {
                        overall_sha1_hash.update(buffer);
                        overall_ed2k_hash.update(buffer);
                    }
                    pieces.add(hash);
                    if (listener != null) {
                        listener.pieceHashed(pieces.size());
                    }
                    buffer_pos = 0;
                }
            } else {
                break;
            }
        }
        if (do_other_per_file_hash) {
            per_file_sha1_digest = sha1_hash.getDigest();
            per_file_ed2k_digest = ed2k_hash.getDigest();
        }
    } catch (TOTorrentException e) {
        throw (e);
    } catch (Throwable e) {
        throw (new TOTorrentException("TOTorrentFileHasher: file read fails '" + e.toString() + "'", TOTorrentException.RT_READ_FAILS));
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }
    return (file_length);
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) ED2KHasher(com.biglybt.core.util.ED2KHasher) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SHA1Hasher(com.biglybt.core.util.SHA1Hasher) FileInputStream(java.io.FileInputStream) TOTorrentException(com.biglybt.core.torrent.TOTorrentException)

Example 4 with SHA1Hasher

use of com.biglybt.core.util.SHA1Hasher in project BiglyBT by BiglySoftware.

the class UDPConnectionSet method writeHeaderEnd.

protected int writeHeaderEnd(ByteBuffer buffer, boolean randomise_size) throws IOException {
    if (randomise_size) {
        int pad = random.nextInt(8);
        for (int i = 0; i < pad; i++) {
            buffer.put((byte) 0);
        }
    }
    short total_length = (short) buffer.position();
    buffer.position(12);
    buffer.putShort((short) (total_length + 4));
    // hash includes real sequence + header content but obviously not including the hash
    byte[] buffer_bytes = buffer.array();
    SHA1Hasher hasher = new SHA1Hasher();
    hasher.update(buffer_bytes, 4, 4);
    hasher.update(buffer_bytes, 12, total_length - 12);
    byte[] hash = hasher.getDigest();
    buffer.position(total_length);
    buffer.put(hash, 0, 4);
    total_length += 4;
    // don't encrypt the sequence numbers
    header_cipher_out.processBytes(buffer_bytes, 12, total_length - 12, buffer_bytes, 12);
    if (total_length > MAX_HEADER) {
        Debug.out("MAX_HEADER exceeded!!!!");
        throw (new IOException("MAX_HEADER exceeded"));
    }
    return (total_length);
}
Also used : SHA1Hasher(com.biglybt.core.util.SHA1Hasher) IOException(java.io.IOException)

Example 5 with SHA1Hasher

use of com.biglybt.core.util.SHA1Hasher in project BiglyBT by BiglySoftware.

the class UDPConnectionSet method setSecret.

protected void setSecret(UDPConnection connection, byte[] session_secret) {
    try {
        if (connection == lead_connection) {
            if (manager.trace()) {
                trace("crypto done");
            }
            SHA1Hasher hasher = new SHA1Hasher();
            hasher.update(KEYA_IV);
            hasher.update(session_secret);
            byte[] a_key = hasher.getDigest();
            hasher = new SHA1Hasher();
            hasher.update(KEYB_IV);
            hasher.update(session_secret);
            byte[] b_key = hasher.getDigest();
            hasher = new SHA1Hasher();
            hasher.update(KEYC_IV);
            hasher.update(session_secret);
            byte[] c_key = hasher.getDigest();
            hasher = new SHA1Hasher();
            hasher.update(KEYD_IV);
            hasher.update(session_secret);
            byte[] d_key = hasher.getDigest();
            // for RC4 enc/dec is irrelevant
            RC4Engine rc4_engine_a = getCipher(a_key);
            RC4Engine rc4_engine_b = getCipher(b_key);
            RC4Engine rc4_engine_c = getCipher(c_key);
            RC4Engine rc4_engine_d = getCipher(d_key);
            if (lead_connection.isIncoming()) {
                header_cipher_out = rc4_engine_a;
                header_cipher_in = rc4_engine_b;
                out_seq_generator = new SequenceGenerator(new Random(bytesToLong(d_key)), rc4_engine_c, false);
                in_seq_generator = new SequenceGenerator(new Random(bytesToLong(c_key)), rc4_engine_d, true);
                random = new Random(bytesToLong(d_key, 8));
            } else {
                header_cipher_out = rc4_engine_b;
                header_cipher_in = rc4_engine_a;
                in_seq_generator = new SequenceGenerator(new Random(bytesToLong(d_key)), rc4_engine_c, true);
                out_seq_generator = new SequenceGenerator(new Random(bytesToLong(c_key)), rc4_engine_d, false);
                random = new Random(bytesToLong(c_key, 8));
            }
            // as the first packet each way is crypto we skip a sequence number from each generator
            // to represent this and initialise the last-in-order seq appropriately so a sensible value is
            // spliced into the next packet
            out_seq_generator.getNextSequenceNumber();
            int[] initial_in_seqs = in_seq_generator.getNextSequenceNumber();
            receive_last_inorder_alt_sequence = initial_in_seqs[3];
            crypto_done = true;
        } else if (!crypto_done) {
            Debug.out("Secondary setSecret but crypto not done");
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        connection.close("Crypto problems: " + Debug.getNestedExceptionMessage(e));
    }
}
Also used : SHA1Hasher(com.biglybt.core.util.SHA1Hasher) RC4Engine(org.gudy.bouncycastle.crypto.engines.RC4Engine)

Aggregations

SHA1Hasher (com.biglybt.core.util.SHA1Hasher)6 IOException (java.io.IOException)2 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 ED2KHasher (com.biglybt.core.util.ED2KHasher)1 StringPattern (com.biglybt.ui.common.util.StringPattern)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Iterator (java.util.Iterator)1 RC4Engine (org.gudy.bouncycastle.crypto.engines.RC4Engine)1