PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String hashedPassword = passwordEncoder.encode(yourpassword);
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; public class PrintBCryptString { public static void main(String[] args) { PasswordEncoder encoder = new BCryptPasswordEncoder(12); System.out.println(encoder.matches("type here some string", encoder.encode("type here some string"))); System.out.println(encoder.encode("type here some string")); } }
decodedZookeeperPassword = PasswordEncoder.decode(zookeeperPassword); } else if (userProps.containsKey(DEFAULT_ADMIN_USER)) { String passwordAndRole = userProps.getProperty(DEFAULT_ADMIN_USER).trim(); decodedZookeeperPassword = passwordAndRole.substring(0, passwordAndRole.indexOf(ROLE_DELIMITER)); } else { decodedZookeeperPassword = PasswordEncoder.encode(CreateEnsembleOptions.generatePassword()); Hashtable<String, Object> zkProperties = new Hashtable<>(); zkProperties.put("zookeeper.url", zookeeperUrl); zkProperties.put("zookeeper.password", PasswordEncoder.encode(decodedZookeeperPassword)); zkConfugiration.update(zkProperties);
@Override public String getZookeeperPassword() { assertValid(); String rawZookeeperPassword = getZookeeperInfo("zookeeper.password"); if (rawZookeeperPassword != null) { return PasswordEncoder.decode(rawZookeeperPassword); } else { return null; } }
/** * Encodes a String into a base 64 String. The resulting encoding is chunked at 76 bytes. * <p/> * * @param s String to encode. * @return encoded string. */ public static String encode(String s) { return shouldEncodePassword(s) ? PREFIX + Base64Encoder.encode(s) : s; }
/** * Decodes a base 64 String into a String. * <p/> * * @param s String to decode. * @return encoded string. * @throws java.lang.IllegalArgumentException * thrown if the given byte array was not valid com.sun.syndication.io.impl.Base64 encoding. */ public static String decode(String s) throws IllegalArgumentException { return shouldDecodePassword(s) ? Base64Encoder.decode(s.substring(PREFIX.length())) : s; }
private String getPassword(FabricService fabricService) { try { String pw = getStringData(fabricService.adapt(CuratorFramework.class), AUTHENTICATION_CRYPT_PASSWORD.getPath()); // the password may be encoded, so we need to decode if needed return PasswordEncoder.decode(pw); } catch (Exception e) { throw FabricException.launderThrowable(e); } }
/** * Encodes a String into a base 64 String. The resulting encoding is chunked at 76 bytes. * <p/> * * @param s String to encode. * @return encoded string. */ public static String encode(String s) { return shouldEncodePassword(s) ? PREFIX + Base64Encoder.encode(s) : s; }
/** * Decodes a base 64 String into a String. * <p/> * * @param s String to decode. * @return encoded string. * @throws java.lang.IllegalArgumentException * thrown if the given byte array was not valid com.sun.syndication.io.impl.Base64 encoding. */ public static String decode(String s) throws IllegalArgumentException { return shouldDecodePassword(s) ? Base64Encoder.decode(s.substring(PREFIX.length())) : s; }
String password = "p4ssword"; PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String encodedPassword = passwordEncoder.encode(password);
@Produces @Singleton public CuratorFramework create(@ServiceName("ZOOKEEPER") String url, @New CuratorConfig config) throws IOException, InterruptedException { LOG.info("Connecting to ZooKeeper URL: {}", url); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(url) .connectionTimeoutMs(config.getConnectionTimeOut()) .sessionTimeoutMs(config.getSessionTimeout()) .retryPolicy(new RetryNTimes(config.getRetryMax(), config.getRetryInterval())); if (!Strings.isNullOrBlank(config.getPassword())) { byte[] auth = (DEFAULT_AUTH_USER + ":" + PasswordEncoder.decode(config.getPassword())).getBytes(); builder = builder.authorization(DEFAULT_AUTH_SCHEME, auth); } CuratorFramework curatorFramework = builder.build(); curatorFramework.start(); return curatorFramework; }
@Test public void passwordShouldMatch() { PasswordEncoder passwordEncoder = new BCryptPasswordEncoder() String plainPassword = "secret"; String hashedPassword = passwordEncoder.encode(plainPassword); assertThat(hashedPassword, matchesPassword(plainPassword)); }
void configureInternal(Map<String, ?> conf) throws Exception { configuration = configurer.configure(conf, this); if (Strings.isNullOrBlank(runtimeId)) { throw new IllegalArgumentException("Runtime id must not be null or empty."); } if (Strings.isNullOrBlank(localResolver)) { localResolver = globalResolver; } String decodedZookeeperPassword = null; Properties userProps = new Properties(); try { userProps.load(new File(confDir , "users.properties")); } catch (IOException e) { LOGGER.warn("Failed to load users from etc/users.properties. No users will be imported.", e); } if (Strings.isNotBlank(zookeeperPassword)) { decodedZookeeperPassword = PasswordEncoder.decode(zookeeperPassword); } else if (userProps.containsKey(DEFAULT_ADMIN_USER)) { String passwordAndRole = userProps.getProperty(DEFAULT_ADMIN_USER).trim(); decodedZookeeperPassword = passwordAndRole.substring(0, passwordAndRole.indexOf(ROLE_DELIMITER)); } else { decodedZookeeperPassword = PasswordEncoder.encode(CreateEnsembleOptions.generatePassword()); } if (userProps.isEmpty()) { userProps.put(DEFAULT_ADMIN_USER, decodedZookeeperPassword+ ROLE_DELIMITER + DEFAULT_ADMIN_ROLE); } options = CreateEnsembleOptions.builder().bindAddress(bindAddress).agentEnabled(agentAutoStart).ensembleStart(ensembleAutoStart).zookeeperPassword(decodedZookeeperPassword) .zooKeeperServerPort(zookeeperServerPort).zooKeeperServerConnectionPort(zookeeperServerConnectionPort).autoImportEnabled(profilesAutoImport) .importPath(profilesAutoImportPath).resolver(localResolver).globalResolver(globalResolver).users(userProps).profiles(profiles).version(version).build(); }
@Override protected Object doExecute() throws Exception { if (exists(getCurator(), ZkPath.AUTHENTICATION_CRYPT_PASSWORD.getPath()) != null) { System.out.println(PasswordEncoder.decode(getStringData(getCurator(), ZkPath.AUTHENTICATION_CRYPT_PASSWORD.getPath()))); } return null; } }
@Autowired private UserDetailsManager users; @Autowired private PasswordEncoder enc; @RequestMapping(...) public String handle(Model model) { users.createUser(new User(username, enc.encode(password), authorities); return "view"; }
/** * Builds a {@link org.apache.curator.framework.CuratorFramework} from the specified {@link java.util.Map<String, ?>}. */ private synchronized CuratorFramework buildCuratorFramework(CuratorConfig curatorConfig) { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .canBeReadOnly(true) .ensembleProvider(new FixedEnsembleProvider(curatorConfig.getZookeeperUrl())) .connectionTimeoutMs(curatorConfig.getZookeeperConnectionTimeOut()) .sessionTimeoutMs(curatorConfig.getZookeeperSessionTimeout()) .retryPolicy(new RetryNTimes(curatorConfig.getZookeeperRetryMax(), curatorConfig.getZookeeperRetryInterval())); if (!Strings.isNullOrEmpty(curatorConfig.getZookeeperPassword())) { String scheme = "digest"; byte[] auth = ("fabric:" + PasswordEncoder.decode(curatorConfig.getZookeeperPassword())).getBytes(); builder = builder.authorization(scheme, auth).aclProvider(aclProvider.get()); } CuratorFramework framework = builder.build(); // ENTESB-2111: don't register SCR-bound ConnectionStateListeners here, rather // invoke them once in State.stateChanged() // for (ConnectionStateListener listener : connectionStateListeners) { // framework.getConnectionStateListenable().addListener(listener); // } return framework; }
String password = "testPassword"; PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String encodedPassword = passwordEncoder.encode(password);