本文整理汇总了Java中org.apache.shiro.codec.Hex类的典型用法代码示例。如果您正苦于以下问题:Java Hex类的具体用法?Java Hex怎么用?Java Hex使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Hex类属于org.apache.shiro.codec包,在下文中一共展示了Hex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAesCipherService
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
@Test
public void testAesCipherService(){
AesCipherService aesCipherService = new AesCipherService();
//设置key长度
aesCipherService.setKeySize(128);
Key key = aesCipherService.generateNewKey();
String text = "hello";
//加密
String encrpText = aesCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
//解密
String text2 = new String(aesCipherService.decrypt(Hex.decode(encrpText), key.getEncoded()).getBytes());
Assert.assertEquals(text, text2);
}
示例2: getCredentials
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
/**
*
* @param info the AuthenticationInfo from which to retrieve the credentials which assumed to be in already-hashed form.
* @return a {@link Hash Hash} instance representing the given AuthenticationInfo's stored credentials.
*/
protected Object getCredentials(AuthenticationInfo info) {
Object credentials = info.getCredentials();
byte[] storedBytes = toBytes(credentials);
if (credentials instanceof String || credentials instanceof char[]) {
//account.credentials were a char[] or String, so
//we need to do text decoding first:
if (isStoredCredentialsHexEncoded()) {
storedBytes = Hex.decode(storedBytes);
} else {
storedBytes = Base64.decode(storedBytes);
}
}
AbstractHash hash = newHashInstance();
hash.setBytes(storedBytes);
return hash;
}
示例3: main
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
public static void main(String[] args) {
String password = "admin";
String cipherText = encrytHex(password);
System.out.println(password + "hex加密之后的密文是:" + cipherText);
String decrptPassword=decryptHex(cipherText);
System.out.println(cipherText + "hex解密之后的密码是:" + decrptPassword);
String cipherText_base64 = encrytBase64(password);
System.out.println(password + "base64加密之后的密文是:" + cipherText_base64);
String decrptPassword_base64=decryptBase64(cipherText_base64);
System.out.println(cipherText_base64 + "base64解密之后的密码是:" + decrptPassword_base64);
String h64= H64.encodeToString(password.getBytes());
System.out.println(h64);
String salt="7road";
String cipherText_md5= new Md5Hash(password,salt,4).toHex();
System.out.println(password+"通过md5加密之后的密文是:"+cipherText_md5);
System.out.println(generateKey());
System.out.println("==========================================================");
AesCipherService aesCipherService=new AesCipherService();
aesCipherService.setKeySize(128);
Key key=aesCipherService.generateNewKey();
String aes_cipherText= aesCipherService.encrypt(password.getBytes(),key.getEncoded()).toHex();
System.out.println(password+" aes加密的密文是:"+aes_cipherText);
String aes_mingwen=new String(aesCipherService.decrypt(Hex.decode(aes_cipherText),key.getEncoded()).getBytes());
System.out.println(aes_cipherText+" aes解密的明文是:"+aes_mingwen);
}
示例4: testBlowfishCipherService
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
@Test
public void testBlowfishCipherService(){
BlowfishCipherService blowfishCipherService = new BlowfishCipherService();
blowfishCipherService.setKeySize(128);
Key key = blowfishCipherService.generateNewKey();
String text = "hello";
//加密
String encrp = blowfishCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
//解密
String text2 = new String(blowfishCipherService.decrypt(Hex.decode(encrp), key.getEncoded()).getBytes());
Assert.assertEquals(text, text2);
}
示例5: testDefaultBlockCipherService
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
@Test
public void testDefaultBlockCipherService(){
//对称加密,使用Java的JCA(javax.crypto.Cipher)加密API,常见的如 'AES', 'Blowfish'
DefaultBlockCipherService blockCipherService = new DefaultBlockCipherService("AES");
blockCipherService.setKeySize(128);
Key key = blockCipherService.generateNewKey();
String text = "hello";
//加密
String encrp = blockCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
//解密
String text2 = new String(blockCipherService.decrypt(Hex.decode(encrp), key.getEncoded()).getBytes());
Assert.assertEquals(text, text2);
}
示例6: verifyPassword
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
private static boolean verifyPassword(byte[] originalHash, ByteSource publicSalt, String password) {
ByteSource privateSalt = ByteSource.Util.bytes(PRIVATE_SALT_BYTES);
DefaultHashService hashService = new DefaultHashService();
hashService.setPrivateSalt(privateSalt);
hashService.setHashIterations(ITERATIONS);
HashRequest.Builder builder = new HashRequest.Builder();
builder.setSource(ByteSource.Util.bytes(password));
builder.setSalt(publicSalt);
Hash comparisonHash = hashService.computeHash(builder.build());
log.info("password: {}", password);
log.info("1 hash: {}", Hex.encodeToString(originalHash));
log.info("2 hash: {}", comparisonHash.toHex());
return Arrays.equals(originalHash, comparisonHash.getBytes());
}
示例7: decryptAES
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
/**
* AES解密
*
* @param content
* @return
*/
public static String decryptAES(String content) {
AesCipherService aesCipherService = new AesCipherService();
aesCipherService.setKeySize(128);
return new String(aesCipherService.decrypt(Hex.decode(content), key.getEncoded()).getBytes());
}
示例8: toHex
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
@Override
public String toHex() {
if ( this.cachedHex == null ) {
this.cachedHex = Hex.encodeToString(getBytes());
}
return this.cachedHex;
}
示例9: testHex
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
@Test
public void testHex(){
String str = "hello";
String hexEncode = Hex.encodeToString(str.getBytes());
String str2 = new String(Hex.decode(hexEncode.getBytes()));
Assert.assertEquals(str, str2);
}
示例10: toBytes
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
protected byte[] toBytes(String sValue) {
if (sValue == null) {
return null;
}
byte[] bytes;
if (sValue.startsWith(HEX_BEGIN_TOKEN)) {
String hex = sValue.substring(HEX_BEGIN_TOKEN.length());
bytes = Hex.decode(hex);
} else {
//assume base64 encoded:
bytes = Base64.decode(sValue);
}
return bytes;
}
示例11: toHex
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
/**
*
* @return a hex-encoded string of the underlying {@link #getBytes byte array}.
*/
public String toHex() {
if (this.hexEncoded == null) {
this.hexEncoded = Hex.encodeToString(getBytes());
}
return this.hexEncoded;
}
示例12: decrypt
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
public String decrypt(String encrypted) {
try {
ByteSource decrypted = cipherService.decrypt(Hex.decode(encrypted), getSecretKey());
return CodecSupport.toString(decrypted.getBytes());
} catch (CryptoException e) {
logger.warn(String.format("Someone tried to use an invalid key [%s]", encrypted));
throw new IllegalArgumentException("Given key is invalid", e);
}
}
示例13: getPasword
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
public ByteSource getPasword() {
if (password != null) {
return ByteSource.Util.bytes( Hex.decode(password) );
} else {
return null;
}
}
示例14: toHex
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
public String toHex() {
if ( this.cachedHex == null ) {
this.cachedHex = Hex.encodeToString(getBytes());
}
return this.cachedHex;
}
示例15: toHex
import org.apache.shiro.codec.Hex; //导入依赖的package包/类
public String toHex() {
if (this.cachedHex == null) {
this.cachedHex = Hex.encodeToString(getBytes());
}
return this.cachedHex;
}