@Test public void html() { String input = "1>2"; String result = Encodes.escapeHtml(input); assertEquals("1>2", result); assertEquals(input, Encodes.unescapeHtml(result)); }
/** 设定安全的密码,生成随机的salt并经过1024次 sha-1 hash */ private void entryptPassword(User user) { byte[] salt = Digests.generateSalt(SALT_SIZE); user.setSalt(Encodes.encodeHex(salt)); byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(), salt, HASH_INTERATIONS); user.setPassword(Encodes.encodeHex(hashPassword)); }
@Test public void urlEncode() { String input = "http://locahost/?q=中文&t=1"; String result = Encodes.urlEncode(input); System.out.println(result); assertEquals(input, Encodes.urlDecode(result)); }
@Test public void base62Encode() { long num = 63; String result = Encodes.encodeBase62(num); assertEquals("11", result); assertEquals(num, Encodes.decodeBase62(result)); }
@Test public void csv() { String input = "haha,kaka"; String result = Encodes.escapeCsv(input); System.out.println(result); assertEquals(input, Encodes.unescapeCsv(result)); // input = "\"ha\"ka"; result = Encodes.escapeCsv(input); System.out.println(result); assertEquals(input, Encodes.unescapeCsv(result)); }
/** 认证回调函数,登录时调用. */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; User user = accountService.findUserByLoginName(token.getUsername()); if (user != null) { byte[] salt = Encodes.decodeHex(user.getSalt()); return new SimpleAuthenticationInfo( new ShiroUser(user.getId(), user.getLoginName(), user.getName()), user.getPassword(), ByteSource.Util.bytes(salt), getName()); } else { return null; } }
@Test public void hexEncode() { String input = "haha,i am a very long message"; String result = Encodes.encodeHex(input.getBytes()); assertEquals(input, new String(Encodes.decodeHex(result))); }
@Test public void base64UrlSafeEncode() { String input = "haha,i am a very long message"; String result = Encodes.encodeUrlSafeBase64(input.getBytes()); assertEquals(input, new String(Encodes.decodeBase64(result))); }