Example usage for java.security NoSuchAlgorithmException NoSuchAlgorithmException

List of usage examples for java.security NoSuchAlgorithmException NoSuchAlgorithmException

Introduction

In this page you can find the example usage for java.security NoSuchAlgorithmException NoSuchAlgorithmException.

Prototype

public NoSuchAlgorithmException() 

Source Link

Document

Constructs a NoSuchAlgorithmException with no detail message.

Usage

From source file:fshp.FSHP.java

/**
 * Returns the hash of <tt>passwd</tt> 
 *
 * @param passwd Byte representation of clear text password.
 * @param salt Byte representation of salt to be used in hashing.
 * @param saltlen Length of the salt. Should be 0 if a salt is already
 *        provided. If salt is null, saltlen bytes of salt will be
 *        auto generated.//from  www  .j  av a2  s  .co  m
 * @param rounds Number of hashing rounds.
 * @param variant FSHP variant indicating the behaviour and/or
 * <ul>
 *        <li><tt>0: SHA-1</tt> <em>(not recommended)</em></li>
 *        <li><tt>1: SHA-256</tt></li>
 *        <li><tt>2: SHA-384</tt></li>
 *        <li><tt>3: SHA-512</tt></li>
 * </ul>
 *
 * @return       FSHP hash of <tt>passwd</tt>
 */
public static String crypt(byte[] passwd, byte[] salt, int saltlen, int rounds, int variant) throws Exception {
    // Ensure we have sane values for salt length and rounds.
    if (saltlen < 0)
        saltlen = 0;
    if (rounds < 1)
        rounds = 1;

    if (salt == null) {
        salt = new byte[saltlen];
        new SecureRandom().nextBytes(salt);
    } else
        saltlen = salt.length;

    HashMap<Integer, String> algoMap = new HashMap<Integer, String>();
    algoMap.put(0, "SHA-1");
    algoMap.put(1, "SHA-256");
    algoMap.put(2, "SHA-384");
    algoMap.put(3, "SHA-512");

    MessageDigest md;
    try {
        if (!algoMap.containsKey(variant))
            throw new NoSuchAlgorithmException();

        md = MessageDigest.getInstance(algoMap.get(variant));
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("Unsupported FSHP variant " + variant);
    }

    md.update(salt);
    md.update(passwd);
    byte[] digest = md.digest();

    for (int i = 1; i < rounds; i++) {
        md.reset();
        md.update(digest);
        digest = md.digest();
    }

    String meta = "{FSHP" + variant + "|" + saltlen + "|" + rounds + "}";

    byte[] saltdigest = new byte[salt.length + digest.length];
    System.arraycopy(salt, 0, saltdigest, 0, salt.length);
    System.arraycopy(digest, 0, saltdigest, salt.length, digest.length);

    byte[] b64saltdigest = Base64.encodeBase64(saltdigest);

    return meta + new String(b64saltdigest, "US-ASCII");
}

From source file:ch.cern.security.saml2.utils.UrlUtils.java

/**
 * Preprocess the request and invokes the verification:
 * /*w w  w.  j  a v a2s  .  c  o m*/
 * @param request
 * @param isDebugEnabled
 * @return
 * @throws Exception
 */
public static boolean verify(HttpServletRequest request, boolean isDebugEnabled) throws Exception {

    StringBuffer data = new StringBuffer();
    String sigAlg = null;
    String signature = null;
    data.append(Constants.SAML_REQUEST);
    data.append(EQUAL);

    if (isDebugEnabled)
        nc.notice("Query String: " + request.getQueryString());

    if (request.getParameter(Constants.SAML_REQUEST) != null) {
        if (isDebugEnabled)
            nc.notice(request.getParameter(Constants.SAML_REQUEST));
        data.append(LogoutUtils.urlEncode(request.getParameter(Constants.SAML_REQUEST),
                Constants.CHARACTER_ENCODING));
    } else {
        nc.error("NO " + Constants.SAML_REQUEST + " parameter");
        throw new Exception("NO " + Constants.SAML_REQUEST + " parameter");
    }

    // Get the sigAlg, it should match with the one declared as context
    // param
    sigAlg = request.getParameter(Constants.SIG_ALG);
    if (isDebugEnabled)
        nc.notice(sigAlg);
    if (((String) request.getSession().getServletContext().getAttribute(Constants.SIG_ALG)).equals(sigAlg)) {
        data.append(AMPERSAND);
        data.append(Constants.SIG_ALG);
        data.append(EQUAL);
        data.append(LogoutUtils.urlEncode(sigAlg, Constants.CHARACTER_ENCODING));
    } else {
        throw new NoSuchAlgorithmException();
    }

    // Get the signature and decode it in Base64
    Base64 base64 = new Base64();
    if (request.getParameter(SIGNATURE) != null) {
        if (isDebugEnabled)
            nc.notice("Signature: " + request.getParameter(SIGNATURE));
        signature = request.getParameter(SIGNATURE);
    } else {
        nc.error("NO " + SIGNATURE + " parameter");
        throw new Exception("NO " + SIGNATURE + " parameter");
    }

    if (isDebugEnabled)
        nc.notice("Data to verify: " + data.toString());

    // Verify
    return SignatureUtils.verify(data.toString().getBytes(), (byte[]) base64.decode(signature.getBytes()),
            (PublicKey) request.getSession().getServletContext().getAttribute(Constants.IDP_PUBLIC_KEY));
}

From source file:be.fedict.commons.eid.jca.BeIDKeyStore.java

@Override
public void engineLoad(final LoadStoreParameter param)
        throws IOException, NoSuchAlgorithmException, CertificateException {
    LOG.debug("engineLoad"); /*
                             * Allows for a KeyStore to be re-loaded several
                             * times.//  ww  w  . j ava 2s . co  m
                             */
    this.beIDCard = null;
    this.authnCertificateChain = null;
    this.signCertificateChain = null;
    this.rrnCertificateChain = null;
    this.authnCertificate = null;
    this.signCertificate = null;
    this.citizenCaCertificate = null;
    this.rootCaCertificate = null;
    this.rrnCertificate = null;
    if (null == param) {
        return;
    }
    if (param instanceof BeIDKeyStoreParameter) {
        this.keyStoreParameter = (BeIDKeyStoreParameter) param;
        return;
    }
    if (param instanceof JFrame) {
        this.keyStoreParameter = new BeIDKeyStoreParameter();
        JFrame frame = (JFrame) param;
        this.keyStoreParameter.setParentComponent(frame);
        return;
    }
    throw new NoSuchAlgorithmException();
}

From source file:com.netscape.cmsutil.crypto.CryptoUtil.java

public static KeyPair generateECCKeyPair(CryptoToken token, String curveName,
        org.mozilla.jss.crypto.KeyPairGeneratorSpi.Usage[] usage_ops,
        org.mozilla.jss.crypto.KeyPairGeneratorSpi.Usage[] usage_mask, boolean temporary, int sensitive,
        int extractable)
        throws NotInitializedException, NoSuchTokenException, NoSuchAlgorithmException, TokenException {

    KeyPairAlgorithm alg = KeyPairAlgorithm.EC;
    KeyPairGenerator keygen = token.getKeyPairGenerator(alg);

    keygen.setKeyPairUsages(usage_ops, usage_mask);
    keygen.setKeyPairUsages(usage_ops, usage_mask);
    keygen.temporaryPairs(temporary);/*w  ww.  j  a v a  2s . co  m*/

    if (sensitive == 1)
        keygen.sensitivePairs(true);
    else if (sensitive == 0)
        keygen.sensitivePairs(false);

    if (extractable == 1)
        keygen.extractablePairs(true);
    else if (extractable == 0)
        keygen.extractablePairs(false);

    //        logger.debug("CryptoUtil: generateECCKeyPair: curve = " + curveName);
    int curveCode = 0;
    try {
        curveCode = keygen.getCurveCodeByName(curveName);
    } catch (Exception e) {
        //            logger.debug("CryptoUtil: generateECCKeyPair: " + e.toString());
        throw new NoSuchAlgorithmException();
    }
    keygen.initialize(curveCode);

    //        logger.debug("CryptoUtil: generateECCKeyPair: after KeyPairGenerator initialize with:" + curveName);
    KeyPair pair = keygen.genKeyPair();

    return pair;
}

From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestConsoleRSAKeyPairGenerator.java

@Test
public void testRun03a() throws Exception {
    this.console = EasyMock.createMockBuilder(ConsoleRSAKeyPairGenerator.class)
            .withConstructor(RSAKeyPairGeneratorInterface.class, TextInterfaceDevice.class,
                    CommandLineParser.class)
            .withArgs(this.generator, this.device, new GnuParser())
            .addMockedMethod("processCommandLineOptions", String[].class).addMockedMethod("doInteractive")
            .addMockedMethod("doCommandLine").createStrictMock();

    String[] arguments = new String[] { "-private" };

    this.console.processCommandLineOptions(arguments);
    EasyMock.expectLastCall();//from  w  w  w .jav  a 2s .  co  m
    this.console.doCommandLine();
    EasyMock.expectLastCall()
            .andThrow(new RSA2048NotSupportedException("Message 03.", new NoSuchAlgorithmException()));
    this.device.printErrLn("Message 03.");
    EasyMock.expectLastCall();
    this.device.exit(51);
    EasyMock.expectLastCall();

    EasyMock.replay(this.console, this.generator, this.device);

    try {
        this.console.run(arguments);
    } finally {
        EasyMock.verify(this.console);
    }
}

From source file:com.netscape.cmsutil.crypto.CryptoUtil.java

/**
 * getHMACAlgorithmOID returns OID of the HMAC algorithm name
 *
 * @param name name of the HMAC algorithm
 * @return OID of the HMAC algorithm/* w w  w  . j  ava2  s  . c  o  m*/
 */
public static OBJECT_IDENTIFIER getHMACAlgorithmOID(String name) throws NoSuchAlgorithmException {
    OBJECT_IDENTIFIER oid = null;
    if (name != null) {
        if (name.equals("SHA-256-HMAC")) {
            oid = (HMACAlgorithm.SHA256).toOID();
        } else if (name.equals("SHA-384-HMAC")) {
            oid = (HMACAlgorithm.SHA384).toOID();
        } else if (name.equals("SHA-512-HMAC")) {
            oid = (HMACAlgorithm.SHA512).toOID();
        }
    }
    if (oid == null) {
        throw new NoSuchAlgorithmException();
    }
    return oid;
}

From source file:com.netscape.cmsutil.crypto.CryptoUtil.java

/**
 * getHashAlgorithmOID returns OID of the hashing algorithm name
 *
 * @param name name of the hashing algorithm
 * @return OID of the hashing algorithm//  ww w .  j  a  v  a 2 s.c om
 *
 */
public static OBJECT_IDENTIFIER getHashAlgorithmOID(String name) throws NoSuchAlgorithmException {
    OBJECT_IDENTIFIER oid = null;
    if (name != null) {
        if (name.equals("SHA-256")) {
            oid = (DigestAlgorithm.SHA256).toOID();
        } else if (name.equals("SHA-384")) {
            oid = (DigestAlgorithm.SHA384).toOID();
        } else if (name.equals("SHA-512")) {
            oid = (DigestAlgorithm.SHA512).toOID();
        }
    }
    if (oid == null) {
        throw new NoSuchAlgorithmException();
    }
    return oid;
}

From source file:com.netscape.cmsutil.crypto.CryptoUtil.java

/**
 * getNameFromHashAlgorithm returns the hashing algorithm name
 * from input Algorithm/* w  w  w  .  ja va2  s  . co  m*/
 *
 * @param ai the hashing algorithm AlgorithmIdentifier
 * @return name of the hashing algorithm
 *
 */
public static String getNameFromHashAlgorithm(AlgorithmIdentifier ai) throws NoSuchAlgorithmException {
    logger.debug("CryptoUtil: getNameFromHashAlgorithm: " + ai.getOID().toString());
    if (ai != null) {
        if (ai.getOID().equals((DigestAlgorithm.SHA256).toOID())) {
            return "SHA-256";
        } else if (ai.getOID().equals((DigestAlgorithm.SHA384).toOID())) {
            return "SHA-384";
        } else if (ai.getOID().equals((DigestAlgorithm.SHA512).toOID())) {
            return "SHA-512";
        }
    }
    throw new NoSuchAlgorithmException();
}

From source file:com.netscape.cmsutil.crypto.CryptoUtil.java

public static OBJECT_IDENTIFIER getOID(KeyWrapAlgorithm kwAlg) throws NoSuchAlgorithmException {
    String name = kwAlg.toString();
    if (name.equals(KeyWrapAlgorithm.AES_KEY_WRAP_PAD.toString()))
        return KW_AES_KEY_WRAP_PAD;
    if (name.equals(KeyWrapAlgorithm.AES_CBC_PAD.toString()))
        return KW_AES_CBC_PAD;
    if (name.equals(KeyWrapAlgorithm.DES3_CBC_PAD.toString()))
        return KW_DES_CBC_PAD;
    if (name.equals(KeyWrapAlgorithm.DES_CBC_PAD.toString()))
        return KW_DES_CBC_PAD;

    throw new NoSuchAlgorithmException();
}

From source file:com.netscape.cmsutil.crypto.CryptoUtil.java

public static KeyWrapAlgorithm getKeyWrapAlgorithmFromOID(String wrapOID) throws NoSuchAlgorithmException {
    OBJECT_IDENTIFIER oid = new OBJECT_IDENTIFIER(wrapOID);
    if (oid.equals(KW_AES_KEY_WRAP_PAD))
        return KeyWrapAlgorithm.AES_KEY_WRAP_PAD;

    if (oid.equals(KW_AES_CBC_PAD))
        return KeyWrapAlgorithm.AES_CBC_PAD;

    if (oid.equals(KW_DES_CBC_PAD))
        return KeyWrapAlgorithm.DES3_CBC_PAD;

    throw new NoSuchAlgorithmException();
}