All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bitplan.mediawiki.japi.user.Crypt Maven / Gradle / Ivy

Go to download

Java library to call Mediawiki API described at http://www.mediawiki.org/wiki/API:Main_page

There is a newer version: 0.2.3
Show newest version
/**
 *
 * This file is part of the https://github.com/WolfgangFahl/Mediawiki-Japi open source project
 *
 * Copyright 2015-2017 BITPlan GmbH https://github.com/BITPlan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 *  You may obtain a copy of the License at
 *
 *  http:www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bitplan.mediawiki.japi.user;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Encryption class to be use for password encryption for test cases
 * @author wf
 *
 */
public class Crypt {

	private char[] cypher;
	byte[] salt;

	/**
	 * @return the cypher
	 */
	public String getCypher() {
		return new String(cypher);
	}
	
	/**
	 * return the salt
	 * @return the salt
	 */
	public String getSalt() {
		return new String(salt);
	}

	/**
	 * @param cypher the cypher to set
	 */
	public void setCypher(char[] cypher) {
		this.cypher = cypher;
	}

	/**
	 * create me from a password and salt
	 * 
	 * @param pCypher
	 * @param pSalt
	 */
	Crypt(String pCypher, String pSalt) {
		this.setCypher(pCypher.toCharArray());
		this.salt = pSalt.getBytes();
	}

	/**
	 * generate a Random key
	 * @param pLength
	 * @return the reandom key with the given length
	 */
	public static String generateRandomKey(int pLength) {
    int asciiFirst = 48;
    int asciiLast = 122;
    Integer[] exceptions = { 58,59,60,61,62,63,91,92,93,94,96 };

    List exceptionsList = Arrays.asList(exceptions);
    SecureRandom random = new SecureRandom();
    StringBuilder builder = new StringBuilder();
    for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy