---
title: "Shiro User Manual-Command Line Hasher"
author: "Technoboy"
date: "2013-04-19T11:49:03Z"
tags: []
categories: ["企业架构"]
slug: "shiro-user-manual-command-line-hasher"
draft: false
lastmod: "2013-04-14T17:18:09Z"
id: 1847184
url: "https://technoboy.iteye.com/blog/1847184"
---

1. Overview
 Shiro1.2及其以后版本，可以通过命令行对字符串和任何类型的资源进行哈希操作。

2. Usage
 下载shiro-tools-hasher-version-cli.jar，如果是通过原文件编译或Maven下载的Shiro，进入buildroot/tools/hasher/target目录，然后执行：

```java

java -jar shiro-tools-hasher-X.X.X-cli.jar

```

 将显示所有对密码进行标准或复杂哈希的选项。

3. Common Scenarios
 请仔细阅读上面命令的说明，它提供了详细的清单指令，将根据你的需求，使用不同的哈希算法。为了方便，我们提供了一些用途/场景的参考。

 3.1 shiro.ini User Passwords
  在shiro.ini文件的部分，使用hash后的密码进行配置。上面的命令加选项-p(或--password)：

```java

> java -jar shiro-tools-hasher-X.X.X-cli.jar -p

```

 它会提示你输入密码并确认：

```java

Password to hash:
Password to hash (confirm):

```

 然后会显示加密后的密码：

```java

$shiro1$SHA-256$500000$eWpVX2tGX7WCP2J+jMCNqw==$it/NRclMOHrfOvhAEFZ0mxIZRdbcfqIBdwdwdDXW2dM=

```

 将这个值写到的密码部分：

```java

[users]
...
user1 = $shiro1$SHA-256$500000$eWpVX2tGX7WCP2J+jMCNqw==$it/NRclMOHrfOvhAEFZ0mxIZRdbcfqIBdwdwdDXW2dM=

```

 然后配置iniRealm，使用CredentialsMatcher即可:

```java

[main]
...
passwordMatcher = org.apache.shiro.authc.credential.PasswordMatcher
iniRealm.credentialsMatcher = $passwordMatcher
...

```


 3.2 MD5 checksum(MD5校验)
 可以使用JVM支持的任何hash算法，默认为
MD5，它可以用于文件的校验。

```java

> java -jar shiro-tools-hasher-X.X.X-cli.jar -r RESOURCE_PATH

```

 -r后为资源的位置，默认为文件系统，可以通过资源前缀指定不同的位置：
 
```java

<command> -r fileInCurrentDirectory.txt
<command> -r ../../relativePathFile.xml
<command> -r ~/documents/myfile.pdf
<command> -r /usr/local/logs/absolutePathFile.log
<command> -r url:http://foo.com/page.html
<command> -r classpath:/WEB-INF/lib/something.jar

```