com.mysql.cj.exceptions.AssertionFailedException Java Examples

The following examples show how to use com.mysql.cj.exceptions.AssertionFailedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StatementImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Close any open result sets that have been 'held open'
 */
protected void closeAllOpenResults() throws SQLException {
    JdbcConnection locallyScopedConn = this.connection;

    if (locallyScopedConn == null) {
        return; // already closed
    }

    synchronized (locallyScopedConn.getConnectionMutex()) {
        if (this.openResults != null) {
            for (ResultSetInternalMethods element : this.openResults) {
                try {
                    element.realClose(false);
                } catch (SQLException sqlEx) {
                    AssertionFailedException.shouldNotHappen(sqlEx);
                }
            }

            this.openResults.clear();
        }
    }
}
 
Example #2
Source File: StatementImpl.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Close any open result sets that have been 'held open'
 * 
 * @throws SQLException
 *             if an error occurs
 */
protected void closeAllOpenResults() throws SQLException {
    JdbcConnection locallyScopedConn = this.connection;

    if (locallyScopedConn == null) {
        return; // already closed
    }

    synchronized (locallyScopedConn.getConnectionMutex()) {
        if (this.openResults != null) {
            for (ResultSetInternalMethods element : this.openResults) {
                try {
                    element.realClose(false);
                } catch (SQLException sqlEx) {
                    AssertionFailedException.shouldNotHappen(sqlEx);
                }
            }

            this.openResults.clear();
        }
    }
}
 
Example #3
Source File: MessageConstants.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static Class<? extends GeneratedMessage> getMessageClassForType(int type) {
    Class<? extends GeneratedMessage> messageClass = MessageConstants.MESSAGE_TYPE_TO_CLASS.get(type);
    if (messageClass == null) {
        // check if there's a mapping that we don't explicitly handle
        ServerMessages.Type serverMessageMapping = ServerMessages.Type.valueOf(type);
        throw AssertionFailedException.shouldNotHappen("Unknown message type: " + type + " (server messages mapping: " + serverMessageMapping + ")");
    }
    return messageClass;
}
 
Example #4
Source File: Security.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Hashing for MySQL-4.1 authentication. Algorithm is as follows (c.f. <i>sql/auth/password.c</i>):
 *
 * <pre>
 * SERVER: public_seed=create_random_string()
 * send(public_seed)
 *
 * CLIENT: recv(public_seed)
 * hash_stage1=sha1("password")
 * hash_stage2=sha1(hash_stage1)
 * reply=xor(hash_stage1, sha1(public_seed,hash_stage2))
 * send(reply)
 * </pre>
 * 
 * @param password
 *            password
 * @param seed
 *            seed
 * @return bytes
 */
public static byte[] scramble411(byte[] password, byte[] seed) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException ex) {
        throw new AssertionFailedException(ex);
    }

    byte[] passwordHashStage1 = md.digest(password);
    md.reset();

    byte[] passwordHashStage2 = md.digest(passwordHashStage1);
    md.reset();

    md.update(seed);
    md.update(passwordHashStage2);

    byte[] toBeXord = md.digest();

    int numToXor = toBeXord.length;

    for (int i = 0; i < numToXor; i++) {
        toBeXord[i] = (byte) (toBeXord[i] ^ passwordHashStage1[i]);
    }

    return toBeXord;
}
 
Example #5
Source File: XProtocol.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public byte[] readAuthenticateContinue() {
    try {
        AuthenticateContinue msg = (AuthenticateContinue) this.reader.readMessage(null, ServerMessages.Type.SESS_AUTHENTICATE_CONTINUE_VALUE).getMessage();
        byte[] data = msg.getAuthData().toByteArray();
        if (data.length != 20) {
            throw AssertionFailedException.shouldNotHappen("Salt length should be 20, but is " + data.length);
        }
        return data;
    } catch (IOException e) {
        throw new XProtocolError(e.getMessage(), e);
    }
}
 
Example #6
Source File: MessageConstants.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public static Class<? extends GeneratedMessageV3> getMessageClassForType(int type) {
    Class<? extends GeneratedMessageV3> messageClass = MessageConstants.MESSAGE_TYPE_TO_CLASS.get(type);
    if (messageClass == null) {
        // check if there's a mapping that we don't explicitly handle
        ServerMessages.Type serverMessageMapping = ServerMessages.Type.forNumber(type);
        throw AssertionFailedException.shouldNotHappen("Unknown message type: " + type + " (server messages mapping: " + serverMessageMapping + ")");
    }
    return messageClass;
}
 
Example #7
Source File: CreateIndexParams.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param indexName
 *            index name
 * @param jsonIndexDefinition
 *            special JSON document containing index definition; see {@link Collection#createIndex(String, String)} description
 */
public CreateIndexParams(String indexName, String jsonIndexDefinition) {
    if (jsonIndexDefinition == null || jsonIndexDefinition.trim().length() == 0) {
        throw new XDevAPIError(Messages.getString("CreateIndexParams.0", new String[] { "jsonIndexDefinition" }));
    }
    try {
        init(indexName, JsonParser.parseDoc(new StringReader(jsonIndexDefinition)));
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #8
Source File: AddStatementImpl.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public AddStatement add(String jsonString) {
    try {
        DbDoc doc = JsonParser.parseDoc(new StringReader(jsonString));
        return add(doc);
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #9
Source File: JsonParser.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create {@link DbDoc} object from JSON string.
 * 
 * @param jsonString
 *            JSON string representing a document
 * @return New {@link DbDoc} object initialized by parsed JSON string.
 */
public static DbDoc parseDoc(String jsonString) {
    try {
        return JsonParser.parseDoc(new StringReader(jsonString));
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #10
Source File: CollectionImpl.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Result addOrReplaceOne(String id, String jsonString) {
    if (id == null) {
        throw new XDevAPIError(Messages.getString("CreateTableStatement.0", new String[] { "id" }));
    }
    if (jsonString == null) {
        throw new XDevAPIError(Messages.getString("CreateTableStatement.0", new String[] { "jsonString" }));
    }
    try {
        return addOrReplaceOne(id, JsonParser.parseDoc(new StringReader(jsonString)));
    } catch (IOException e) {
        throw AssertionFailedException.shouldNotHappen(e);
    }
}
 
Example #11
Source File: CollectionImpl.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Result replaceOne(String id, String jsonString) {
    if (id == null) {
        throw new XDevAPIError(Messages.getString("CreateTableStatement.0", new String[] { "id" }));
    }
    if (jsonString == null) {
        throw new XDevAPIError(Messages.getString("CreateTableStatement.0", new String[] { "jsonString" }));
    }
    try {
        return replaceOne(id, JsonParser.parseDoc(new StringReader(jsonString)));
    } catch (IOException e) {
        throw AssertionFailedException.shouldNotHappen(e);
    }
}
 
Example #12
Source File: CollectionImpl.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AddStatement add(String... jsonString) {
    try {
        DbDoc[] docs = new DbDoc[jsonString.length];
        for (int i = 0; i < jsonString.length; i++) {
            docs[i] = JsonParser.parseDoc(new StringReader(jsonString[i]));
        }
        return add(docs);
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #13
Source File: DbDocValueFactory.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Interpret the given byte array as a string. This value factory needs to know the encoding to interpret the string. The default (null) will interpret the
 * byte array using the platform encoding.
 */
@Override
public DbDoc createFromBytes(byte[] bytes, int offset, int length) {
    try {
        return JsonParser.parseDoc(new StringReader(StringUtils.toString(bytes, offset, length, this.encoding)));
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #14
Source File: CreateIndexParams.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CreateIndexParams(String indexName, String jsonIndexDefinition) {
    if (jsonIndexDefinition == null || jsonIndexDefinition.trim().length() == 0) {
        throw new XDevAPIError(Messages.getString("CreateIndexParams.0", new String[] { "jsonIndexDefinition" }));
    }
    try {
        init(indexName, JsonParser.parseDoc(new StringReader(jsonIndexDefinition)));
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #15
Source File: AddStatementImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public AddStatement add(String jsonString) {
    try {
        DbDoc doc = JsonParser.parseDoc(new StringReader(jsonString));
        return add(doc);
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #16
Source File: JsonParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static DbDoc parseDoc(String jsonString) {
    try {
        return JsonParser.parseDoc(new StringReader(jsonString));
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #17
Source File: CollectionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Result addOrReplaceOne(String id, String jsonString) {
    if (id == null) {
        throw new XDevAPIError(Messages.getString("CreateTableStatement.0", new String[] { "id" }));
    }
    if (jsonString == null) {
        throw new XDevAPIError(Messages.getString("CreateTableStatement.0", new String[] { "jsonString" }));
    }
    try {
        return addOrReplaceOne(id, JsonParser.parseDoc(new StringReader(jsonString)));
    } catch (IOException e) {
        throw AssertionFailedException.shouldNotHappen(e);
    }
}
 
Example #18
Source File: CollectionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Result replaceOne(String id, String jsonString) {
    if (id == null) {
        throw new XDevAPIError(Messages.getString("CreateTableStatement.0", new String[] { "id" }));
    }
    if (jsonString == null) {
        throw new XDevAPIError(Messages.getString("CreateTableStatement.0", new String[] { "jsonString" }));
    }
    try {
        return replaceOne(id, JsonParser.parseDoc(new StringReader(jsonString)));
    } catch (IOException e) {
        throw AssertionFailedException.shouldNotHappen(e);
    }
}
 
Example #19
Source File: CollectionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public AddStatement add(String... jsonString) {
    try {
        DbDoc[] docs = new DbDoc[jsonString.length];
        for (int i = 0; i < jsonString.length; i++) {
            docs[i] = JsonParser.parseDoc(new StringReader(jsonString[i]));
        }
        return add(docs);
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #20
Source File: DbDocValueFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Interpret the given byte array as a string. This value factory needs to know the encoding to interpret the string. The default (null) will interpet the
 * byte array using the platform encoding.
 */
@Override
public DbDoc createFromBytes(byte[] bytes, int offset, int length) {
    try {
        return JsonParser.parseDoc(new StringReader(StringUtils.toString(bytes, offset, length, this.encoding)));
    } catch (IOException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #21
Source File: Security.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hashing for MySQL-4.1 authentication. Algorithm is as follows (c.f. <i>sql/auth/password.c</i>):
 *
 * <pre>
 * SERVER: public_seed=create_random_string()
 * send(public_seed)
 *
 * CLIENT: recv(public_seed)
 * hash_stage1=sha1("password")
 * hash_stage2=sha1(hash_stage1)
 * reply=xor(hash_stage1, sha1(public_seed,hash_stage2))
 * send(reply)
 * </pre>
 */
public static byte[] scramble411(byte[] password, byte[] seed) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException ex) {
        throw new AssertionFailedException(ex);
    }

    byte[] passwordHashStage1 = md.digest(password);
    md.reset();

    byte[] passwordHashStage2 = md.digest(passwordHashStage1);
    md.reset();

    md.update(seed);
    md.update(passwordHashStage2);

    byte[] toBeXord = md.digest();

    int numToXor = toBeXord.length;

    for (int i = 0; i < numToXor; i++) {
        toBeXord[i] = (byte) (toBeXord[i] ^ passwordHashStage1[i]);
    }

    return toBeXord;
}
 
Example #22
Source File: XProtocol.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public byte[] readAuthenticateContinue() {
    try {
        AuthenticateContinue msg = (AuthenticateContinue) this.reader.readMessage(null, ServerMessages.Type.SESS_AUTHENTICATE_CONTINUE_VALUE).getMessage();
        byte[] data = msg.getAuthData().toByteArray();
        if (data.length != 20) {
            throw AssertionFailedException.shouldNotHappen("Salt length should be 20, but is " + data.length);
        }
        return data;
    } catch (IOException e) {
        throw new XProtocolError(e.getMessage(), e);
    }
}
 
Example #23
Source File: MySQLJDBCReflections.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
void registerExceptionsForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJCommunicationsException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, CJConnectionFeatureNotAvailableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJTimeoutException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJPacketTooBigException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, AssertionFailedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ClosedOnExpiredPasswordException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ConnectionIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataConversionException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataReadException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataTruncationException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DeadlockTimeoutRollbackMarker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, FeatureNotAvailableException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, InvalidConnectionAttributeException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NumberOutOfRange.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, OperationCancelledException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PasswordExpiredException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PropertyNotModifiableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, RSAException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, SSLParamsException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StatementIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StreamingNotifiable.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, UnableToConnectException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, UnsupportedConnectionStringException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, WrongArgumentException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, "com.mysql.cj.jdbc.MysqlXAException"));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, StandardLoadBalanceExceptionChecker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NdbLoadBalanceExceptionChecker.class.getName()));
}
 
Example #24
Source File: Security.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Scrambling for caching_sha2_password plugin.
 * 
 * <pre>
 * Scramble = XOR(SHA2(password), SHA2(SHA2(SHA2(password)), Nonce))
 * </pre>
 * 
 * @throws DigestException
 */
public static byte[] scrambleCachingSha2(byte[] password, byte[] seed) throws DigestException {
    /*
     * Server does it in 4 steps (see sql/auth/sha2_password_common.cc Generate_scramble::scramble method):
     * 
     * SHA2(src) => digest_stage1
     * SHA2(digest_stage1) => digest_stage2
     * SHA2(digest_stage2, m_rnd) => scramble_stage1
     * XOR(digest_stage1, scramble_stage1) => scramble
     */
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException ex) {
        throw new AssertionFailedException(ex);
    }

    byte[] dig1 = new byte[CACHING_SHA2_DIGEST_LENGTH];
    byte[] dig2 = new byte[CACHING_SHA2_DIGEST_LENGTH];
    byte[] scramble1 = new byte[CACHING_SHA2_DIGEST_LENGTH];

    // SHA2(src) => digest_stage1
    md.update(password, 0, password.length);
    md.digest(dig1, 0, CACHING_SHA2_DIGEST_LENGTH);
    md.reset();

    // SHA2(digest_stage1) => digest_stage2
    md.update(dig1, 0, dig1.length);
    md.digest(dig2, 0, CACHING_SHA2_DIGEST_LENGTH);
    md.reset();

    // SHA2(digest_stage2, m_rnd) => scramble_stage1
    md.update(dig2, 0, dig1.length);
    md.update(seed, 0, seed.length);
    md.digest(scramble1, 0, CACHING_SHA2_DIGEST_LENGTH);

    // XOR(digest_stage1, scramble_stage1) => scramble
    byte[] mysqlScrambleBuff = new byte[CACHING_SHA2_DIGEST_LENGTH];
    xorString(dig1, mysqlScrambleBuff, scramble1, CACHING_SHA2_DIGEST_LENGTH);

    return mysqlScrambleBuff;
}
 
Example #25
Source File: XProtocolDecoder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public <T> T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // packed BCD format (c.f. wikipedia)
        // TODO: optimization possibilities include using int/long if the digits is < X and scale = 0
        byte scale = inputStream.readRawByte();
        // we allocate an extra char for the sign
        CharBuffer unscaledString = CharBuffer.allocate(2 * inputStream.getBytesUntilLimit());
        unscaledString.position(1);
        byte sign = 0;
        // read until we encounter the sign bit
        while (true) {
            int b = 0xFF & inputStream.readRawByte();
            if ((b >> 4) > 9) {
                sign = (byte) (b >> 4);
                break;
            }
            unscaledString.append((char) ((b >> 4) + '0'));
            if ((b & 0x0f) > 9) {
                sign = (byte) (b & 0x0f);
                break;
            }
            unscaledString.append((char) ((b & 0x0f) + '0'));
        }
        if (inputStream.getBytesUntilLimit() > 0) {
            throw AssertionFailedException
                    .shouldNotHappen("Did not read all bytes while decoding decimal. Bytes left: " + inputStream.getBytesUntilLimit());
        }
        switch (sign) {
            case 0xa:
            case 0xc:
            case 0xe:
            case 0xf:
                unscaledString.put(0, '+');
                break;
            case 0xb:
            case 0xd:
                unscaledString.put(0, '-');
                break;
        }
        // may have filled the CharBuffer or one remaining. need to remove it before toString()
        int characters = unscaledString.position();
        unscaledString.clear(); // reset position
        BigInteger unscaled = new BigInteger(unscaledString.subSequence(0, characters).toString());
        return vf.createFromBigDecimal(new BigDecimal(unscaled, scale));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example #26
Source File: XProtocolDecoder.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T> T decodeDecimal(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    try {
        CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
        // packed BCD format (c.f. wikipedia)
        // TODO: optimization possibilities include using int/long if the digits is < X and scale = 0
        byte scale = inputStream.readRawByte();
        // we allocate an extra char for the sign
        CharBuffer unscaledString = CharBuffer.allocate(2 * inputStream.getBytesUntilLimit());
        unscaledString.position(1);
        byte sign = 0;
        // read until we encounter the sign bit
        while (true) {
            int b = 0xFF & inputStream.readRawByte();
            if ((b >> 4) > 9) {
                sign = (byte) (b >> 4);
                break;
            }
            unscaledString.append((char) ((b >> 4) + '0'));
            if ((b & 0x0f) > 9) {
                sign = (byte) (b & 0x0f);
                break;
            }
            unscaledString.append((char) ((b & 0x0f) + '0'));
        }
        if (inputStream.getBytesUntilLimit() > 0) {
            throw AssertionFailedException
                    .shouldNotHappen("Did not read all bytes while decoding decimal. Bytes left: " + inputStream.getBytesUntilLimit());
        }
        switch (sign) {
            case 0xa:
            case 0xc:
            case 0xe:
            case 0xf:
                unscaledString.put(0, '+');
                break;
            case 0xb:
            case 0xd:
                unscaledString.put(0, '-');
                break;
        }
        // may have filled the CharBuffer or one remaining. need to remove it before toString()
        int characters = unscaledString.position();
        unscaledString.clear(); // reset position
        BigInteger unscaled = new BigInteger(unscaledString.subSequence(0, characters).toString());
        return vf.createFromBigDecimal(new BigDecimal(unscaled, scale));
    } catch (IOException e) {
        throw new DataReadException(e);
    }
}
 
Example #27
Source File: Security.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Scrambling for caching_sha2_password plugin.
 * 
 * <pre>
 * Scramble = XOR(SHA2(password), SHA2(SHA2(SHA2(password)), Nonce))
 * </pre>
 * 
 * @param password
 *            password
 * @param seed
 *            seed
 * @return bytes
 * 
 * @throws DigestException
 *             if an error occurs
 */
public static byte[] scrambleCachingSha2(byte[] password, byte[] seed) throws DigestException {
    /*
     * Server does it in 4 steps (see sql/auth/sha2_password_common.cc Generate_scramble::scramble method):
     * 
     * SHA2(src) => digest_stage1
     * SHA2(digest_stage1) => digest_stage2
     * SHA2(digest_stage2, m_rnd) => scramble_stage1
     * XOR(digest_stage1, scramble_stage1) => scramble
     */
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException ex) {
        throw new AssertionFailedException(ex);
    }

    byte[] dig1 = new byte[CACHING_SHA2_DIGEST_LENGTH];
    byte[] dig2 = new byte[CACHING_SHA2_DIGEST_LENGTH];
    byte[] scramble1 = new byte[CACHING_SHA2_DIGEST_LENGTH];

    // SHA2(src) => digest_stage1
    md.update(password, 0, password.length);
    md.digest(dig1, 0, CACHING_SHA2_DIGEST_LENGTH);
    md.reset();

    // SHA2(digest_stage1) => digest_stage2
    md.update(dig1, 0, dig1.length);
    md.digest(dig2, 0, CACHING_SHA2_DIGEST_LENGTH);
    md.reset();

    // SHA2(digest_stage2, m_rnd) => scramble_stage1
    md.update(dig2, 0, dig1.length);
    md.update(seed, 0, seed.length);
    md.digest(scramble1, 0, CACHING_SHA2_DIGEST_LENGTH);

    // XOR(digest_stage1, scramble_stage1) => scramble
    byte[] mysqlScrambleBuff = new byte[CACHING_SHA2_DIGEST_LENGTH];
    xorString(dig1, mysqlScrambleBuff, scramble1, CACHING_SHA2_DIGEST_LENGTH);

    return mysqlScrambleBuff;
}
 
Example #28
Source File: AsyncMessageReader.java    From FoxTelem with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Parse a message.
 * 
 * @param messageClass
 *            class extending {@link GeneratedMessageV3}
 * @param buf
 *            message buffer
 * @return {@link GeneratedMessageV3}
 */
private GeneratedMessageV3 parseMessage(Class<? extends GeneratedMessageV3> messageClass, ByteBuffer buf) {
    try {
        Parser<? extends GeneratedMessageV3> parser = MessageConstants.MESSAGE_CLASS_TO_PARSER.get(messageClass);
        return parser.parseFrom(CodedInputStream.newInstance(buf));
    } catch (InvalidProtocolBufferException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}
 
Example #29
Source File: AsyncMessageReader.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Parse a message.
 * 
 * @param messageClass
 *            class extending {@link GeneratedMessage}
 * @param buf
 *            message buffer
 * @return {@link GeneratedMessage}
 */
private GeneratedMessage parseMessage(Class<? extends GeneratedMessage> messageClass, ByteBuffer buf) {
    try {
        Parser<? extends GeneratedMessage> parser = MessageConstants.MESSAGE_CLASS_TO_PARSER.get(messageClass);
        return parser.parseFrom(CodedInputStream.newInstance(buf));
    } catch (InvalidProtocolBufferException ex) {
        throw AssertionFailedException.shouldNotHappen(ex);
    }
}