Java Code Examples for org.apache.mina.util.Base64#decodeBase64()

The following examples show how to use org.apache.mina.util.Base64#decodeBase64() . 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: CipherManager.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the token is valid. True if it is. False otherwise.
 * @param token
 * @return
 */
public UserId checkEncryptedUserToken(String token) {
	if ( !StringUtil.checkNotEmpty(token) ) return null;
	byte[] encrypted = Base64.decodeBase64(token.getBytes());
	try {
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE, this.key);
		byte[] textBytes = cipher.doFinal(encrypted);
		ByteBuffer buf = ByteBuffer.wrap(textBytes);
		long timeMillis = buf.getLong();
		byte[] userIdBytes = new byte[buf.remaining()];
		buf.get(userIdBytes);
		UserId userId = UserId.fromBytes(userIdBytes);
		if ( userId != null && timeMillis + CipherManager.timeout > System.currentTimeMillis() ) {
			return userId;
		} else {
			logger.debug("User's token is timeout");
		}
	} catch (Exception e) {
		logger.debug("checkEncryptedUserToken exception: {} ", e.getMessage());
		logger.debug("User's token is invalid");
	}
	return null;
}
 
Example 2
Source File: CipherManager.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the token is valid. True if it is. False otherwise.
 * @param token
 * @return
 */
public Account checkEncryptedAccountToken(String token) {
	if ( !StringUtil.checkNotEmpty(token) ) return null;
	byte[] encrypted = Base64.decodeBase64(token.getBytes());
	try {
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE, this.key);
		byte[] textBytes = cipher.doFinal(encrypted);
		ByteBuffer buf = ByteBuffer.wrap(textBytes);
		long timeMillis = buf.getLong();
		byte[] accountIdBytes = new byte[buf.remaining()];
		buf.get(accountIdBytes);
		if ( accountIdBytes != null && accountIdBytes.length>0 && 
				timeMillis + CipherManager.timeout > System.currentTimeMillis() ) {
			String accountId = new String(accountIdBytes);
			Account account = AccountManager.getInstance().queryAccountById(accountId);
			return account;
		} else {
			logger.debug("Account's token is timeout");
		}
	} catch (Exception e) {
		logger.debug("checkEncryptedUserToken exception: {} ", e.getMessage());
		logger.debug("Account's token is invalid");
	}
	return null;
}
 
Example 3
Source File: CodeGenerator_new.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
private void parseErrors(String error)
{
	if (error.contains(ERROR_HOOK))
	{
		String[] lines = error.split("\n");
		for (String line : lines)
		{
			if (line.contains(ERROR_HOOK))
			{
				int index = line.indexOf(ERROR_HOOK);
				index += ERROR_HOOK.length()+1;
				String data = line.substring(index);
				String[] vals = StringUtil.split(data, ":");
				String base64 = StringUtil.join(":", vals, 4);
				byte[] bytes = Base64.decodeBase64(base64.getBytes());
				String value = new String(bytes);
				error = "Invalid value in column '"+vals[3]+"': "+value;
				if (!alertCollector.contains(AlertType.ERROR, error)) {
                       alertCollector.add(new Alert(Long.parseLong(vals[0]), Long.parseLong(vals[1]), vals[2], vals[3], error));
				}
			}
		}
		return;
	}
       alertCollector.add(new Alert(error));

}