org.apache.shiro.crypto.hash.Hash类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(295)

本文整理了Java中org.apache.shiro.crypto.hash.Hash类的一些代码示例,展示了Hash类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Hash类的具体详情如下:
包路径:org.apache.shiro.crypto.hash.Hash
类名称:Hash

Hash介绍

[英]A Cryptographic Hash represents a one-way conversion algorithm that transforms an input source to an underlying byte array. Hex and Base64-encoding output of the hashed bytes are automatically supported by the inherited #toHex() and #toBase64() methods.

The bytes returned by the parent interface's #getBytes() are the hashed value of the original input source, also known as the 'checksum' or 'digest'.
[中]加密哈希表示将输入源转换为底层字节数组的单向转换算法。继承的#toHex()和#toBase64()方法自动支持哈希字节的十六进制和Base64编码输出。
父接口的#getBytes()返回的字节是原始输入源的哈希值,也称为“校验和”或“摘要”。

代码示例

代码示例来源:origin: apache/shiro

String hashed = hex ? hash.toHex() : hash.toBase64();
System.out.print(hex ? "Hex: " : "Base64: ");
System.out.println(hashed);

代码示例来源:origin: apache/shiro

public String format(Hash hash) {
  if (hash == null) {
    return null;
  }
  String algorithmName = hash.getAlgorithmName();
  ByteSource salt = hash.getSalt();
  int iterations = hash.getIterations();
  StringBuilder sb = new StringBuilder(MCF_PREFIX).append(algorithmName).append(TOKEN_DELIMITER).append(iterations).append(TOKEN_DELIMITER);
  if (salt != null) {
    sb.append(salt.toBase64());
  }
  sb.append(TOKEN_DELIMITER);
  sb.append(hash.toBase64());
  return sb.toString();
}

代码示例来源:origin: apache/shiro

/**
 * Returns {@code true} if the specified object is a Hash and its {@link #getBytes byte array} is identical to
 * this Hash's byte array, {@code false} otherwise.
 *
 * @param o the object (Hash) to check for equality.
 * @return {@code true} if the specified object is a Hash and its {@link #getBytes byte array} is identical to
 *         this Hash's byte array, {@code false} otherwise.
 */
public boolean equals(Object o) {
  if (o instanceof Hash) {
    Hash other = (Hash) o;
    return MessageDigest.isEqual(getBytes(), other.getBytes());
  }
  return false;
}

代码示例来源:origin: apache/shiro

protected HashRequest buildHashRequest(ByteSource plaintext, Hash saved) {
  //keep everything from the saved hash except for the source:
  return new HashRequest.Builder().setSource(plaintext)
      //now use the existing saved data:
      .setAlgorithmName(saved.getAlgorithmName())
      .setSalt(saved.getSalt())
      .setIterations(saved.getIterations())
      .build();
}

代码示例来源:origin: org.apache.knox/gateway-provider-security-shiro

@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token, Object ldapPrincipal, Object ldapCredentials, LdapContext ldapContext) throws NamingException {
 HashRequest.Builder builder = new HashRequest.Builder();
 Hash credentialsHash = hashService.computeHash(builder.setSource(token.getCredentials()).setAlgorithmName(HASHING_ALGORITHM).build());
 return new SimpleAuthenticationInfo(token.getPrincipal(), credentialsHash.toHex(), credentialsHash.getSalt(), getName());
}

代码示例来源:origin: apache/shiro

/**
   * Returns {@code hash != null ? hash.toHex() : null}.
   *
   * @param hash the hash instance to format into a String.
   * @return {@code hash != null ? hash.toHex() : null}.
   */
  public String format(Hash hash) {
    return hash != null ? hash.toHex() : null;
  }
}

代码示例来源:origin: dschadow/JavaSecurity

public static void main(String[] args) {
  String password = "SHA-512 hash sample text";
  Hash hash = calculateHash(password);
  boolean correct = verifyPassword(hash.getBytes(), hash.getSalt(), password);
  log.info("Entered password is correct: {}", correct);
}

代码示例来源:origin: dschadow/JavaSecurity

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());
  }
}

代码示例来源:origin: Waffle/waffle

/**
 * Builds the authentication info.
 *
 * @param token
 *            the token
 * @param principal
 *            the principal
 * @return the authentication info
 */
private AuthenticationInfo buildAuthenticationInfo(final UsernamePasswordToken token, final Object principal) {
  AuthenticationInfo authenticationInfo;
  final HashingPasswordService hashService = this.getHashService();
  if (hashService != null) {
    final Hash hash = hashService.hashPassword(token.getPassword());
    final ByteSource salt = hash.getSalt();
    authenticationInfo = new SimpleAuthenticationInfo(principal, hash, salt, AbstractWaffleRealm.REALM_NAME);
  } else {
    final Object creds = token.getCredentials();
    authenticationInfo = new SimpleAuthenticationInfo(principal, creds, AbstractWaffleRealm.REALM_NAME);
  }
  return authenticationInfo;
}

代码示例来源:origin: apache/shiro

/**
   * Returns {@code hash != null ? hash.toBase64() : null}.
   *
   * @param hash the hash instance to format into a String.
   * @return {@code hash != null ? hash.toBase64() : null}.
   */
  public String format(Hash hash) {
    return hash != null ? hash.toBase64() : null;
  }
}

代码示例来源:origin: apache/knox

@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token, Object ldapPrincipal, Object ldapCredentials, LdapContext ldapContext) throws NamingException {
 HashRequest.Builder builder = new HashRequest.Builder();
 Hash credentialsHash = hashService.computeHash(builder.setSource(token.getCredentials()).setAlgorithmName(HASHING_ALGORITHM).build());
 return new SimpleAuthenticationInfo(token.getPrincipal(), credentialsHash.toHex(), credentialsHash.getSalt(), getName());
}

代码示例来源:origin: org.apache.shiro/shiro-core

protected HashRequest buildHashRequest(ByteSource plaintext, Hash saved) {
  //keep everything from the saved hash except for the source:
  return new HashRequest.Builder().setSource(plaintext)
      //now use the existing saved data:
      .setAlgorithmName(saved.getAlgorithmName())
      .setSalt(saved.getSalt())
      .setIterations(saved.getIterations())
      .build();
}

代码示例来源:origin: org.apache.shiro/shiro-crypto-hash

/**
   * Returns {@code hash != null ? hash.toHex() : null}.
   *
   * @param hash the hash instance to format into a String.
   * @return {@code hash != null ? hash.toHex() : null}.
   */
  public String format(Hash hash) {
    return hash != null ? hash.toHex() : null;
  }
}

代码示例来源:origin: com.github.waffle/waffle-shiro

/**
 * Builds the authentication info.
 *
 * @param token
 *            the token
 * @param principal
 *            the principal
 * @return the authentication info
 */
private AuthenticationInfo buildAuthenticationInfo(final UsernamePasswordToken token, final Object principal) {
  AuthenticationInfo authenticationInfo;
  final HashingPasswordService hashService = this.getHashService();
  if (hashService != null) {
    final Hash hash = hashService.hashPassword(token.getPassword());
    final ByteSource salt = hash.getSalt();
    authenticationInfo = new SimpleAuthenticationInfo(principal, hash, salt, AbstractWaffleRealm.REALM_NAME);
  } else {
    final Object creds = token.getCredentials();
    authenticationInfo = new SimpleAuthenticationInfo(principal, creds, AbstractWaffleRealm.REALM_NAME);
  }
  return authenticationInfo;
}

代码示例来源:origin: org.apache.shiro/shiro-crypto-hash

/**
   * Returns {@code hash != null ? hash.toBase64() : null}.
   *
   * @param hash the hash instance to format into a String.
   * @return {@code hash != null ? hash.toBase64() : null}.
   */
  public String format(Hash hash) {
    return hash != null ? hash.toBase64() : null;
  }
}

代码示例来源:origin: magefree/mage

public AuthorizedUser(String name, Hash hash, String email) {
  this.name = name;
  this.password = hash.toBase64();
  this.salt = hash.getSalt().toBase64();
  this.hashAlgorithm = hash.getAlgorithmName();
  this.hashIterations = hash.getIterations();
  this.email = email;
  this.chatLockedUntil = null;
  this.active = true;
  this.lockedUntil = null;
}

代码示例来源:origin: org.apache.knox/gateway-provider-security-shiro

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
 UsernamePasswordToken upToken = (UsernamePasswordToken) token;
 UnixUser user = null;
 try {
  user = (new PAM(this.getService())).authenticate(upToken.getUsername(), new String(upToken.getPassword()));
 } catch (PAMException e) {
  handleAuthFailure(token, e.getMessage(), e);
 }
 HashRequest.Builder builder = new HashRequest.Builder();
 Hash credentialsHash = hashService
   .computeHash(builder.setSource(token.getCredentials()).setAlgorithmName(HASHING_ALGORITHM).build());
 /* Coverity Scan CID 1361684 */
 if (credentialsHash == null) {
  handleAuthFailure(token, "Failed to compute hash", null);
 }
 return new SimpleAuthenticationInfo(new UnixUserPrincipal(user), credentialsHash.toHex(), credentialsHash.getSalt(),
   getName());
}

代码示例来源:origin: dschadow/JavaSecurity

private static Hash calculateHash(String password) {
  ByteSource privateSalt = ByteSource.Util.bytes(PRIVATE_SALT_BYTES);
  DefaultHashService hashService = new DefaultHashService();
  hashService.setPrivateSalt(privateSalt);
  hashService.setGeneratePublicSalt(true);
  hashService.setHashIterations(ITERATIONS);
  HashRequest.Builder builder = new HashRequest.Builder();
  builder.setSource(ByteSource.Util.bytes(password));
  Hash hash = hashService.computeHash(builder.build());
  log.info("Hash algorithm {}, iterations {}, public salt {}", hash.getAlgorithmName(), hash.getIterations(), hash.getSalt());
  return hash;
}

代码示例来源:origin: org.apache.shiro/shiro-crypto-hash

String hashed = hex ? hash.toHex() : hash.toBase64();
System.out.print(hex ? "Hex: " : "Base64: ");
System.out.println(hashed);

代码示例来源:origin: apache/shiro

/**
 * Returns {@code true} if the specified object is a Hash and its {@link #getBytes byte array} is identical to
 * this Hash's byte array, {@code false} otherwise.
 *
 * @param o the object (Hash) to check for equality.
 * @return {@code true} if the specified object is a Hash and its {@link #getBytes byte array} is identical to
 *         this Hash's byte array, {@code false} otherwise.
 */
public boolean equals(Object o) {
  if (o instanceof Hash) {
    Hash other = (Hash) o;
    return MessageDigest.isEqual(getBytes(), other.getBytes());
  }
  return false;
}

相关文章