🌹作者主页:青花锁 🌹简介:Java领域优质创作者🏆、Java微服务架构公号作者😄
🌹简历模板、学习资料、面试题库、技术互助
🌹文末获取联系方式 📝
往期热门专栏回顾
专栏 | 描述 |
---|---|
Java项目实战 | 介绍Java组件安装、使用;手写框架等 |
Aws服务器实战 | Aws Linux服务器上操作nginx、git、JDK、Vue |
Java微服务实战 | Java 微服务实战,Spring Cloud Netflix套件、Spring Cloud Alibaba套件、Seata、gateway、shadingjdbc等实战操作 |
Java基础篇 | Java基础闲聊,已出HashMap、String、StringBuffer等源码分析,JVM分析,持续更新中 |
Springboot篇 | 从创建Springboot项目,到加载数据库、静态资源、输出RestFul接口、跨越问题解决到统一返回、全局异常处理、Swagger文档 |
Spring MVC篇 | 从创建Spring MVC项目,到加载数据库、静态资源、输出RestFul接口、跨越问题解决到统一返回 |
华为云服务器实战 | 华为云Linux服务器上操作nginx、git、JDK、Vue等,以及使用宝塔运维操作添加Html网页、部署Springboot项目/Vue项目等 |
Java爬虫 | 通过Java+Selenium+GoogleWebDriver 模拟真人网页操作爬取花瓣网图片、bing搜索图片等 |
Vue实战 | 讲解Vue3的安装、环境配置,基本语法、循环语句、生命周期、路由设置、组件、axios交互、Element-ui的使用等 |
Spring | 讲解Spring(Bean)概念、IOC、AOP、集成jdbcTemplate/redis/事务等 |
前言
俗话说得好,好记性不如烂笔头,尤其是互联网工作项目流动性大,每个人可能管理好几个甚至十多个项目,电脑也是人手好几台,这时候一些工具类就需要记录下来,在我们需要时,及时的拿出来。
HexUtil
直接使用JDK8即可,不需要额外的jar包。
import java.io.UnsupportedEncodingException; public class HexUtil { public static void main(String[] args) throws UnsupportedEncodingException { String hexString = "3058 4645 2030 5846 4520 3058 4645 2030 5846 4520 3058 3731"; System.out.println( new String(HexUtil.HexStringToByte(hexString) , "UTF-8") ); } /** * 16进制字符串转换为Byte型数组16进制源字符串 * * @param hexString 16进制字符串 * @return Byte类型数组 */ public static byte[] HexStringToByte(String hexString) { hexString = hexString.replace(" ", ""); int len = hexString.length(); if (len % 2 != 0) return null; byte[] bufD = new byte[len / 2]; byte[] tmpBuf = hexString.getBytes(); int i = 0, j = 0; for (i = 0; i = 0x30 && tmpBuf[i] = 0x41 && tmpBuf[i] = 0x61 && tmpBuf[i] bufD[j] = (byte) ((tmpBuf[i] public static enum Algorithm { HMAC_MD5("HmacMD5"), HMAC_SHA1("HmacSHA1"), HMAC_SHA256("HmacSHA256"), HMAC_SHA384("HmacSHA384"), HMAC_SHA512("HmacSHA512"); private String value; Algorithm(String value) { this.value = value; } public String getValue() { return value; } } public final static String APP_SECRET = "seelook"; public static void main(String[] args) { String message = "Hello, World!"; try { log.info("HMAC-SHA512 length:{} info:{}", HmacUtil.getHmacSHA512Data(message).length(), HmacUtil.getHmacSHA512Data(message)); log.info("HMAC-SHA384 length:{} info:{}", HmacUtil.getHmacSHA384Data(message).length(), HmacUtil.getHmacSHA384Data(message)); log.info("HMAC-SHA256 length:{} info:{}", HmacUtil.getHmacSHA256Data(message).length(), HmacUtil.getHmacSHA256Data(message)); log.info("HMAC-SHA1 length:{} info:{}", HmacUtil.getHmacSHA1Data(message).length(), HmacUtil.getHmacSHA1Data(message)); log.info("HMAC-MD5 length:{} info:{}", HmacUtil.getHmacMD5Data(message).length(), HmacUtil.getHmacMD5Data(message)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } } public static String getHmacSHA512Data(String data) throws NoSuchAlgorithmException, InvalidKeyException { String instance = Algorithm.HMAC_SHA512.value; return getHmacData(data, instance); } public static String getHmacSHA256Data(String data) throws NoSuchAlgorithmException, InvalidKeyException { String instance = Algorithm.HMAC_SHA256.value; return getHmacData(data, instance); } public static String getHmacSHA1Data(String data) throws NoSuchAlgorithmException, InvalidKeyException { String instance = Algorithm.HMAC_SHA1.value; return getHmacData(data, instance); } public static String getHmacMD5Data(String data) throws NoSuchAlgorithmException, InvalidKeyException { String instance = Algorithm.HMAC_MD5.value; return getHmacData(data, instance); } public static String getHmacSHA384Data(String data) throws NoSuchAlgorithmException, InvalidKeyException { String instance = Algorithm.HMAC_SHA384.value; return getHmacData(data, instance); } private static String getHmacData(String data, String instance) throws NoSuchAlgorithmException, InvalidKeyException { // 生成密钥 SecretKey secretKey = new SecretKeySpec(APP_SECRET.getBytes(), instance); // 创建HMAC-SHA256的Mac实例 Mac mac = Mac.getInstance(instance); // 初始化Mac实例,并设置密钥 mac.init(secretKey); // 计算认证码 byte[] hmac = mac.doFinal(data.getBytes()); return bytesToHex(hmac); } private static SecretKey generateSecretKey(String instance) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(instance); return keyGenerator.generateKey(); } public static String bytesToHex(byte[] bytes) { StringBuilder hexString = new StringBuilder(); for (byte b : bytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } } public static volatile SnowflakeIdWorker idWorker = null; public static synchronized SnowflakeIdWorker getIdWorker() { if (null == idWorker) { idWorker = new SnowflakeIdWorker(0, 0); } return idWorker; } } public class SnowflakeIdWorker { /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L if (workerId maxWorkerId || workerId 0) { expire(key, time); } return count; } catch (Exception e) { log.error(key, e); return 0; } } /** * 获取set缓存的长度 * * @param key 键 */ public long sGetSetSize(String key) { try { return redisTemplate.opsForSet().size(key); } catch (Exception e) { log.error(key, e); return 0; } } /** * 移除值为value的 * * @param key 键 * @param values 值 可以是多个 * @return 移除的个数 */ public long setRemove(String key, Object... values) { try { return redisTemplate.opsForSet().remove(key, values); } catch (Exception e) { log.error(key, e); return 0; } } // ============================zset============================= /** * 根据key获取Set中的所有值 * * @param key 键 */ public Set zSGet(String key) { try { return redisTemplate.opsForSet().members(key); } catch (Exception e) { log.error(key, e); return null; } } /** * 根据value从一个set中查询,是否存在 * * @param key 键 * @param value 值 * @return true 存在 false不存在 */ public boolean zSHasKey(String key, Object value) { try { return redisTemplate.opsForSet().isMember(key, value); } catch (Exception e) { log.error(key, e); return false; } } public Boolean zSSet(String key, Object value, double score) { try { return redisTemplate.opsForZSet().add(key, value, 2); } catch (Exception e) { log.error(key, e); return false; } } /** * 将set数据放入缓存 * * @param key 键 * @param time 时间(秒) * @param values 值 可以是多个 * @return 成功个数 */ public long zSSetAndTime(String key, long time, Object... values) { try { Long count = redisTemplate.opsForSet().add(key, values); if (time > 0) { expire(key, time); } return count; } catch (Exception e) { log.error(key, e); return 0; } } /** * 获取set缓存的长度 * * @param key 键 */ public long zSGetSetSize(String key) { try { return redisTemplate.opsForSet().size(key); } catch (Exception e) { log.error(key, e); return 0; } } /** * 移除值为value的 * * @param key 键 * @param values 值 可以是多个 * @return 移除的个数 */ public long zSetRemove(String key, Object... values) { try { return redisTemplate.opsForSet().remove(key, values); } catch (Exception e) { log.error(key, e); return 0; } } // ===============================list================================= /** * 获取list缓存的内容 * * @param key 键 * @param start 开始 0 是第一个元素 * @param end 结束 -1代表所有值 */ public List lGet(String key, long start, long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { log.error(key, e); return null; } } /** * 获取list缓存的长度 * * @param key 键 */ public long lGetListSize(String key) { try { return redisTemplate.opsForList().size(key); } catch (Exception e) { log.error(key, e); return 0; } } /** * 通过索引 获取list中的值 * * @param key 键 * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index try { return redisTemplate.opsForList().index(key, index); } catch (Exception e) { log.error(key, e); return null; } } /** * 将list放入缓存 * * @param key 键 * @param value 值 */ public boolean lSet(String key, Object value) { try { redisTemplate.opsForList().rightPush(key, value); return true; } catch (Exception e) { log.error(key, e); return false; } } /** * 将list放入缓存 * * @param key 键 * @param value 值 * @param time 时间(秒) */ public boolean lSet(String key, Object value, long time) { try { redisTemplate.opsForList().rightPush(key, value); if (time 0) { expire(key, time); } return true; } catch (Exception e) { log.error(key, e); return false; } } /** * 将list放入缓存 * * @param key 键 * @param value 值 */ public boolean lSet(String key, List value) { try { redisTemplate.opsForList().rightPushAll(key, value); return true; } catch (Exception e) { log.error(key, e); return false; } } /** * 将list放入缓存 * * @param key 键 * @param value 值 * @param time 时间(秒) */ public boolean lSet(String key, List value, long time) { try { redisTemplate.opsForList().rightPushAll(key, value); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { log.error(key, e); return false; } } /** * 根据索引修改list中的某条数据 * * @param key 键 * @param index 索引 * @param value 值 */ public boolean lUpdateIndex(String key, long index, Object value) { try { redisTemplate.opsForList().set(key, index, value); return true; } catch (Exception e) { log.error(key, e); return false; } } /** * 移除N个值为value * * @param key 键 * @param count 移除多少个 * @param value 值 * @return 移除的个数 */ public long lRemove(String key, long count, Object value) { try { return redisTemplate.opsForList().remove(key, count, value); } catch (Exception e) { log.error(key, e); return 0; } } /** * 批量插入数据 */ public void batchInsert(Map map) { redisTemplate.executePipelined((RedisCallback) connection -> { RedisSerializer serializer = redisTemplate.getValueSerializer(); map.forEach((key, value) -> connection.set(serializer.serialize(key), serializer.serialize(value))); return null; }); } /** * 批量插入数据 */ public void batchInsert(List keys, String value, Long time) { //批量set数据 redisTemplate.executePipelined((RedisCallback) connection -> { for (String key : keys) { connection.setEx(key.getBytes(), time, value.getBytes()); } return null; }); } /** * 批量插入数据 */ public void batchInsert(List keys, String value) { // 批量set数据 redisTemplate.executePipelined((RedisCallback) connection -> { for (String key : keys) { connection.set(key.getBytes(), value.getBytes()); } return null; }); } /** * 批量删除 */ public void batchDelete(List keys) { redisTemplate.executePipelined((RedisCallback) connection -> { for (String key : keys) { connection.del(key.getBytes()); } return null; }); } public void multDel(String keys) { redisTemplate.delete(redisTemplate.opsForHash().keys(keys).toString()); } /** * 序列化对象 */ public String serialize(Object result) { return JSON.toJSONString(result); } public Object deserialize(String json, Class clazz) { // 返回结果是list对象 if (clazz.isAssignableFrom(List.class)) { return JSON.parseArray(json, clazz); } return JSON.parseObject(json, clazz); } }
资料获取,更多粉丝福利,关注下方公众号获取
文章版权声明:除非注明,否则均为VPS857原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...