Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 2EC02200B42 for ; Sun, 26 Jun 2016 02:25:39 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 2D6F7160A6B; Sun, 26 Jun 2016 00:25:39 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 73AAD160A66 for ; Sun, 26 Jun 2016 02:25:38 +0200 (CEST) Received: (qmail 36519 invoked by uid 500); 26 Jun 2016 00:25:37 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 36496 invoked by uid 99); 26 Jun 2016 00:25:37 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 26 Jun 2016 00:25:37 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id 59E722C14E1 for ; Sun, 26 Jun 2016 00:25:37 +0000 (UTC) Date: Sun, 26 Jun 2016 00:25:37 +0000 (UTC) From: "Sebb (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Comment Edited] (CRYPTO-81) improve factory api for constructing Random & Cipher MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Sun, 26 Jun 2016 00:25:39 -0000 [ https://issues.apache.org/jira/browse/CRYPTO-81?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15349885#comment-15349885 ] Sebb edited comment on CRYPTO-81 at 6/26/16 12:25 AM: ------------------------------------------------------ If the Crypto code is not intended to be extended by 3rd parties, then it makes sense to use an enum for the implementation classes - they can be enumerated. If not, then it makes more sense to use a String which is either a full class name or an alias. One way to make it easier to find the aliases is to use a separate class for the constants. The Javadoc can then refer to the class, and IDEs should show the relevant entries in the class. was (Author: sebb@apache.org): If the Crypto code is not intended to be extended by 3rd parties, then it makes sebs > improve factory api for constructing Random & Cipher > ---------------------------------------------------- > > Key: CRYPTO-81 > URL: https://issues.apache.org/jira/browse/CRYPTO-81 > Project: Commons Crypto > Issue Type: Bug > Reporter: Xianda Ke > Assignee: Xianda Ke > > currently, the client code to construct a CryptoRandom or CryptoCipher looks like this: > {code} > // code snippet (a) > Properties props = new Properties(); > props.setProperty( > ConfigurationKeys.COMMONS_CRYPTO_SECURE_RANDOM_CLASSES_KEY, > OpensslCryptoRandom.class.getName()); > CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); > {code} > or using configuration file, it looks like : > {code} > # config file > secure.random.classes="org.apache.commons.crypto.random.OpensslCryptoRandom" > {code} > {code} > // code snippet (b) > { > Properties props = loadMyApplicationConfig(); > // ... > } > > { > // bussiness logic ... > CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); > // ... > CryptoCipher cipher = CryptoCipherFactory.getInstance(transform, props); > } > {code} > disadvantages: > 1. if client user just want use openssl engine, trivial stuff in code snippet (a). it looks annoying. > 2. Client user has to use the long long config key string such as "COMMONS_CRYPTO_SECURE_RANDOM_CLASSES_KEY" or full name of classes > Client user has to read source to learn how to config the properties. > 3. the implementation classes such as JavaCryptoRandom, OsCryptoRandom and JavaCryptoRandom are public. > it would be hard to change library implementation in future. > if we just *use a enum (RandomProvider or CryptCipherProvider)* > {code} > // code snippet (c) > // client code looks simple and elegant now: > //RandomProvider.OS or RandomProvider.JAVA > CryptoRandom random = CryptoRandomFactory.getCryptoRandom(RandomProvider.OPENSSL); > {code} > still, client user can use configuration file > {code} > # config file > RandomProvider="OPENSSL" > CryptCipherProvider="JCE" > {code} > {code} > // code snippet > { > Properties props = loadMyApplicationConfig(); > RandomProvider randProvider = RandomProvider.valueOf(props.getProperty(p1)); > CryptoProvider cryptoRrovider =RandomProvider.valueOf(props.getProperty(p1)); > } > { > // bussiness logic ... > CryptoRandom random = CryptoRandomFactory.getCryptoRandom(randProvider); > // ... > CryptoCipher cipher = CryptoCipherFactory.getInstance(transform, cryptoRrovider); > } > {code} > advantages: > 1. Simpler API. snippet (c) is simpler than snippet (a). > 2. Modern IDE will hint that CryptoRandomFactory.getCryptoRandom() need a enum type (RandomProvider). client user do NOT have to search the long key string such as "COMMONS_CRYPTO_SECURE_RANDOM_CLASSES_KEY". Modern IDE will tell client user how to config > 3. we don't have to expose the implementation classes as public -- This message was sent by Atlassian JIRA (v6.3.4#6332)