org.apache.commons.codec.digest.Md5Crypt Java Examples

The following examples show how to use org.apache.commons.codec.digest.Md5Crypt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdminUserServiceImpl.java    From unimall with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean addUser(Long adminId, UserDO user) throws ServiceException {
    if (user == null){
        throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
    }
    if(user.getPhone() == null){
        throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
    }
    if(userMapper.selectCount(new EntityWrapper<UserDO>().eq("phone",user.getPhone())) > 0){
        throw new AdminServiceException(ExceptionDefinition.USER_PHONE_ALREADY_EXIST);
    }
    Date now = new Date();
    user.setId(null);
    user.setPassword(Md5Crypt.md5Crypt(user.getPassword().getBytes(), "$1$" + user.getPhone().substring(0,7)));
    user.setGmtCreate(now);
    user.setGmtUpdate(now);
    return userMapper.insert(user) > 0;
}
 
Example #2
Source File: AdminUserServiceImpl.java    From unimall with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean updateUser(Long adminId, UserDO user) throws ServiceException {
    if (user == null || user.getId() == null){
        throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
    }
    if(user.getPhone() == null){
        throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
    }
    if(userMapper.selectCount(new EntityWrapper<UserDO>().eq("phone",user.getPhone()).notIn("id",user.getId())) > 0){
        throw new AdminServiceException(ExceptionDefinition.USER_PHONE_ALREADY_EXIST);
    }
    Date now = new Date();
    user.setGmtUpdate(now);
    UserDO userDO  = userMapper.selectById(user.getId());
    if(userDO.getPassword().equals(user.getPassword())){
        return userMapper.updateById(user) > 0;
    }
    if(user.getPassword() != null) {
        user.setPassword(Md5Crypt.md5Crypt(user.getPassword().getBytes(), "$1$" + user.getPhone().substring(0, 7)));
    }
    return userMapper.updateById(user) > 0;
}
 
Example #3
Source File: UserServiceImpl.java    From unimall with Apache License 2.0 6 votes vote down vote up
@Override
@Transactional(rollbackFor = Exception.class)
public String resetPassword(String phone, String password, String verifyCode) throws ServiceException {
    //1.校验验证码
    checkVerifyCode(phone, verifyCode);
    //2.校验用户是否存在
    List<UserDO> targetUserList = userMapper.selectList(
            new EntityWrapper<UserDO>()
                    .eq("phone", phone));
    if (CollectionUtils.isEmpty(targetUserList)) {
        throw new AppServiceException(ExceptionDefinition.USER_PHONE_NOT_EXIST);
    }
    Long id = targetUserList.get(0).getId();
    //3.校验成功,重置密码
    UserDO updateUserDO = new UserDO();
    updateUserDO.setId(id);
    updateUserDO.setPassword(Md5Crypt.md5Crypt(password.getBytes(), "$1$" + phone.substring(0, 7)));
    updateUserDO.setGmtUpdate(new Date());
    if (userMapper.updateById(updateUserDO) > 0) {
        cacheComponent.del(VERIFY_CODE_PREFIX + phone);
        return "ok";
    }
    throw new AppServiceException(ExceptionDefinition.USER_UNKNOWN_EXCEPTION);
}
 
Example #4
Source File: DefaultCacheManager.java    From FATE-Serving with Apache License 2.0 6 votes vote down vote up
private String generateRemoteModelInferenceResultCacheKey(FederatedParams  federatedParams){
    Preconditions.checkNotNull(federatedParams);
    Preconditions.checkNotNull(federatedParams.getModelInfo());
    Preconditions.checkNotNull(federatedParams.getFeatureIdMap());
    String namespace = federatedParams.getModelInfo().getNamespace();
    String name = federatedParams.getModelInfo().getName();

    Map  sortedMap = Maps.newTreeMap();
    federatedParams.getFeatureIdMap().forEach((k,v)->{
        sortedMap.put(k,v);
    });
    StringBuffer sb  =  new StringBuffer();
    sb.append(namespace);
    sb.append(name);
    sortedMap.forEach((k,v)->{
        sb.append(k).append(v);
    });
    String md5key = Md5Crypt.md5Crypt(sb.toString().getBytes(), Dict.MD5_SALT);
    return  md5key;
}
 
Example #5
Source File: UserServiceImpl.java    From unimall with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public String register(String phone, String password, String verifyCode, String ip) throws ServiceException {
    //1.校验验证码
    checkVerifyCode(phone, verifyCode);
    //2.校验用户是否存在
    Integer count = userMapper.selectCount(
            new EntityWrapper<UserDO>()
                    .eq("phone", phone));
    if (count > 0) {
        throw new AppServiceException(ExceptionDefinition.USER_PHONE_HAS_EXISTED);
    }
    //3.校验成功,注册用户
    Date now = new Date();
    UserDO userDO = new UserDO();
    userDO.setPhone(phone);
    userDO.setPassword(Md5Crypt.md5Crypt(password.getBytes(), "$1$" + phone.substring(0, 7)));
    userDO.setLastLoginIp(ip);
    userDO.setGmtLastLogin(now);
    userDO.setGmtUpdate(now);
    userDO.setGmtCreate(now);
    userDO.setLoginType(UserLoginType.REGISTER.getCode());
    userMapper.insert(userDO);
    //返回用户DTO
    cacheComponent.del(VERIFY_CODE_PREFIX + phone);
    return null;
}
 
Example #6
Source File: AuthenticationProviderBasic.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public String authenticate(AuthenticationDataSource authData) throws AuthenticationException {
    AuthParams authParams = new AuthParams(authData);
    String userId = authParams.getUserId();
    String password = authParams.getPassword();
    String msg = "Unknown user or invalid password";

    if (users.get(userId) == null) {
        throw new AuthenticationException(msg);
    }

    String encryptedPassword = users.get(userId);

    // For md5 algorithm
    if ((users.get(userId).startsWith("$apr1"))) {
        List<String> splitEncryptedPassword = Arrays.asList(encryptedPassword.split("\\$"));
        if (splitEncryptedPassword.size() != 4 || !encryptedPassword
                .equals(Md5Crypt.apr1Crypt(password.getBytes(), splitEncryptedPassword.get(2)))) {
            throw new AuthenticationException(msg);
        }
    // For crypt algorithm
    } else if (!encryptedPassword.equals(Crypt.crypt(password.getBytes(), encryptedPassword.substring(0, 2)))) {
        throw new AuthenticationException(msg);
    }

    return userId;
}
 
Example #7
Source File: APR1.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
@Override
public String hash(HashString hashString, String password) {
  final String apr1Salt = "$apr1$" + hashString.salt();
  String res = Md5Crypt.apr1Crypt(password, apr1Salt);
  // we need to exclude the salt part
  return res.substring(apr1Salt.length() + 1);
}
 
Example #8
Source File: PasswordEncrypt.java    From sftpserver with Apache License 2.0 5 votes vote down vote up
public PasswordEncrypt(final String key) {
	final byte[] keyBytes = key.getBytes(US_ASCII);
	this.md5 = Md5Crypt.md5Crypt(keyBytes.clone());
	this.apr1 = Md5Crypt.apr1Crypt(keyBytes.clone());
	this.sha256 = Sha2Crypt.sha256Crypt(keyBytes.clone());
	this.sha512 = Sha2Crypt.sha512Crypt(keyBytes.clone());
	Arrays.fill(keyBytes, (byte) 0);
}
 
Example #9
Source File: PasswordEncrypt.java    From sftpserver with Apache License 2.0 5 votes vote down vote up
public static boolean checkPassword(final String crypted, final String key) {
	String crypted2 = null;
	if (crypted == null)
		return false;
	if (crypted.length() < 24)
		return false;
	if (crypted.charAt(0) != '$')
		return false;
	final int offset2ndDolar = crypted.indexOf('$', 1);
	if (offset2ndDolar < 0)
		return false;
	final int offset3ndDolar = crypted.indexOf('$', offset2ndDolar + 1);
	if (offset3ndDolar < 0)
		return false;
	final String salt = crypted.substring(0, offset3ndDolar + 1);
	final byte[] keyBytes = key.getBytes(US_ASCII);
	if (crypted.startsWith("$1$")) { // MD5
		crypted2 = Md5Crypt.md5Crypt(keyBytes.clone(), salt);
	} else if (crypted.startsWith("$apr1$")) { // APR1
		crypted2 = Md5Crypt.apr1Crypt(keyBytes.clone(), salt);
	} else if (crypted.startsWith("$5$")) { // SHA2-256
		crypted2 = Sha2Crypt.sha256Crypt(keyBytes.clone(), salt);
	} else if (crypted.startsWith("$6$")) { // SHA2-512
		crypted2 = Sha2Crypt.sha512Crypt(keyBytes.clone(), salt);
	}
	Arrays.fill(keyBytes, (byte) 0);
	if (crypted2 == null)
		return false;
	return crypted.equals(crypted2);
}
 
Example #10
Source File: UserServiceImpl.java    From unimall with Apache License 2.0 4 votes vote down vote up
@Override
@Transactional
public UserDTO login(String phone, String password, Integer loginType, String raw, String ip) throws ServiceException {
    String cryptPassword = Md5Crypt.md5Crypt(password.getBytes(), "$1$" + phone.substring(0, 7));
    UserDTO userDTO = userMapper.login(phone, cryptPassword);
    if (userDTO == null) {
        throw new AppServiceException(ExceptionDefinition.USER_PHONE_OR_PASSWORD_NOT_CORRECT);
    }
    //检查帐号是否已经冻结
    if (userDTO.getStatus() == 0) {
        throw new AppServiceException(ExceptionDefinition.USER_CAN_NOT_ACTICE);
    }
    if (!StringUtils.isEmpty(raw) && UserLoginType.contains(loginType)) {
        if (loginType == UserLoginType.MP_WEIXIN.getCode()) {
            try {
                JSONObject thirdPartJsonObject = JSONObject.parseObject(raw);
                String code = thirdPartJsonObject.getString("code");
                String body = okHttpClient.newCall(new Request.Builder()
                        .url("https://api.weixin.qq.com/sns/jscode2session?appid=" + (UserLoginType.MP_WEIXIN.getCode() == loginType ? wxMiNiAppid : wxAppAppid) +
                                "&secret=" + (UserLoginType.MP_WEIXIN.getCode() == loginType ? wxMiNiSecret : wxAppSecret) +
                                "&grant_type=authorization_code&js_code=" + code).get().build()).execute().body().string();
                JSONObject jsonObject = JSONObject.parseObject(body);
                Integer errcode = jsonObject.getInteger("errcode");
                if (errcode == null || errcode == 0) {
                    String miniOpenId = jsonObject.getString("openid");
                    //将此次登录的openId,暂且放入user的域里面,支付的时候会用到
                    userDTO.setLoginType(loginType);
                    userDTO.setOpenId(miniOpenId);
                }
            } catch (Exception e) {
                logger.error("[微信第三方登录] 异常", e);
                throw new ThirdPartServiceException(ExceptionDefinition.THIRD_PART_SERVICE_EXCEPTION.getMsg(), ExceptionDefinition.THIRD_PART_SERVICE_EXCEPTION.getCode());
            }
        }
    }
    String accessToken = GeneratorUtil.genSessionId();
    //放入SESSION专用Redis数据源中
    userRedisTemplate.opsForValue().set(Const.USER_REDIS_PREFIX + accessToken, JSONObject.toJSONString(userDTO));
    userDTO.setAccessToken(accessToken);
    UserDO userUpdateDO = new UserDO();
    userUpdateDO.setId(userDTO.getId());
    userUpdateDO.setGmtLastLogin(new Date());
    userUpdateDO.setLastLoginIp(ip);
    userMapper.updateById(userUpdateDO);
    return userDTO;
}
 
Example #11
Source File: ModelService.java    From FATE-Serving with Apache License 2.0 4 votes vote down vote up
private String md5Crypt(PublishRequest req) {
    char[] encryptArray = StringUtils.join(req.getLocal(), req.getRoleMap(), req.getModelMap()).toCharArray();
    Arrays.sort(encryptArray);
    String key = Md5Crypt.md5Crypt(String.valueOf(encryptArray).getBytes(), Dict.MD5_SALT);
    return key;
}
 
Example #12
Source File: Md5CryptUtil.java    From Photo with GNU General Public License v3.0 4 votes vote down vote up
public static String EncryptPassword(String password,String salt){
 final String MAGICPREFIX="$1$";
 return Md5Crypt.md5Crypt(password.getBytes(),MAGICPREFIX+salt);
}
 
Example #13
Source File: PasswordManager.java    From hawkular-metrics with Apache License 2.0 4 votes vote down vote up
private boolean verifyMD5Password(String storedCredential, String passedPassword) {
    // We send in the password presented by the user and use the stored password as the salt
    // If they match, then the password matches the original non-encrypted stored password
    return Md5Crypt.apr1Crypt(passedPassword, storedCredential).equals(storedCredential);
}