外包做了一个项目,使用 Java SpringBoot 来实现,但我们这边使用 PHP 来做后端。 这就涉及到两大平台两种语言的登陆一致性问题
Java 版使用 org.apache.shiro:shiro-spring:1.3.2
版本的 Md5 来实现
/* * This Java source file was generated by the Gradle 'init' task. */ package xxac; import org.apache.shiro.crypto.hash.SimpleHash; import org.apache.shiro.util.ByteSource; public class App { public static void main(String[] args) { //干扰数据 盐 防破解 final String SALT = "2526"; //散列算法类型为MD5 final String ALGORITH_NAME = "md5"; // password final String PASSWORD = "IloveYou520"; //hash的次数 final int HASH_ITERATIONS = 2; String newPassword = new SimpleHash( ALGORITH_NAME, PASSWORD, ByteSource.Util.bytes(SALT), HASH_ITERATIONS ).toHex(); System.out.println(newPassword); } }
运行结果如下
42a0a6347b5a53a109f4aea793466ba8
上面的代码,转换为 PHP 则为
<?php <?php $password = "IloveYou520"; $salt ="2526"; function simple_hash($algo = 'md5', $password = '', $salt = '', $hash_iterations = 2) { $res = ''; $pass = $salt . $password; $encoded = hash($algo, $pass, true); $iteration = $hash_iterations - 1; if($iteration > 0) { for($i = 0; $i < $iteration; $i++) { $encoded = hash($algo, $encoded, true); } } $tmp = unpack('H*', $encoded); if(!empty($tmp) && !empty($tmp[1])) { $res = $tmp[1]; } return $res; } echo simple_hash('md5',$password,$salt);
目前尚无回复