Secret Key Generate Java Docs

Returns a KeyGenerator object that generates secret keys for the specified algorithm. This method traverses the list of registered security Providers, starting with the most preferred Provider. A new KeyGenerator object encapsulating the KeyGeneratorSpi implementation from the first Provider that supports the specified algorithm is returned. An encryption context is a collection of non-secret key-value pairs that represents additional authenticated data. When you use an encryption context to encrypt data, you must specify the same (an exact case-sensitive match) encryption context to decrypt the data. Secret key factories operate only on secret (symmetric) keys. Key factories are bi-directional, i.e., they allow to build an opaque key object from a given key specification (key material), or to retrieve the underlying key material of a key object in a suitable format. See the SecretKeyFactory section in the Java Cryptography Architecture.

This class provides the functionality of a secret (symmetric) key generator.

Key generators are constructed using one of the getInstance class methods of this class.

KeyGenerator objects are reusable, i.e., after a key has been generated, the same KeyGenerator object can be re-used to generate further keys.

There are two ways to generate a key: in an algorithm-independent manner, and in an algorithm-specific manner. The only difference between the two is the initialization of the object:

  • Algorithm-Independent Initialization

    All key generators share the concepts of a keysize and a source of randomness. There is an init method in this KeyGenerator class that takes these two universally shared types of arguments. There is also one that takes just a keysize argument, and uses the SecureRandom implementation of the highest-priority installed provider as the source of randomness (or a system-provided source of randomness if none of the installed providers supply a SecureRandom implementation), and one that takes just a source of randomness.

    Since no other parameters are specified when you call the above algorithm-independent init methods, it is up to the provider what to do about the algorithm-specific parameters (if any) to be associated with each of the keys.

  • Algorithm-Specific Initialization

    For situations where a set of algorithm-specific parameters already exists, there are two init methods that have an AlgorithmParameterSpec argument. One also has a SecureRandom argument, while the other uses the SecureRandom implementation of the highest-priority installed provider as the source of randomness (or a system-provided source of randomness if none of the installed providers supply a SecureRandom implementation).

In case the client does not explicitly initialize the KeyGenerator (via a call to an init method), each provider must supply (and document) a default initialization.

Since:
1.4
See Also:
SecretKey
Constructor Summary
protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider, String algorithm)
Creates a KeyGenerator object.
Method Summary
SecretKeygenerateKey()
Generates a secret key.
StringgetAlgorithm()
Returns the algorithm name of this KeyGenerator object.
static KeyGeneratorgetInstance(String algorithm)
Returns a KeyGenerator object that generates secret keys for the specified algorithm.
static KeyGeneratorgetInstance(String algorithm, Provider provider)
Returns a KeyGenerator object that generates secret keys for the specified algorithm.
static KeyGeneratorgetInstance(String algorithm, String provider)
Returns a KeyGenerator object that generates secret keys for the specified algorithm.
ProvidergetProvider()
Returns the provider of this KeyGenerator object.
voidinit(AlgorithmParameterSpec params)
Initializes this key generator with the specified parameter set.
voidinit(AlgorithmParameterSpec params, SecureRandom random)
Initializes this key generator with the specified parameter set and a user-provided source of randomness.
voidinit(int keysize)
Initializes this key generator for a certain keysize.
voidinit(int keysize, SecureRandom random)
Initializes this key generator for a certain keysize, using a user-provided source of randomness.
voidinit(SecureRandom random)
Initializes this key generator.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Constructor Detail

KeyGenerator

Creates a KeyGenerator object.
Parameters:
keyGenSpi - the delegate
provider - the provider
algorithm - the algorithm
Method Detail

getAlgorithm

Returns the algorithm name of this KeyGenerator object.

This is the same name that was specified in one of the getInstance calls that created this KeyGenerator object.

Returns:
the algorithm name of this KeyGenerator object.

Secret Key Generate Javadocs Free

getInstance

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

This method traverses the list of registered security Providers, starting with the most preferred Provider. A new KeyGenerator object encapsulating the KeyGeneratorSpi implementation from the first Provider that supports the specified algorithm is returned.

Note that the list of registered providers may be retrieved via the Security.getProviders() method.

Secret Key Generate Javadocs In Eclipse

Parameters:
algorithm - the standard name of the requested key algorithm. See Appendix A in the Java Cryptography Architecture Reference Guide for information about standard algorithm names.
Returns:
the new KeyGenerator object.
Throws:
NullPointerException - if the specified algorithm is null.
NoSuchAlgorithmException - if no Provider supports a KeyGeneratorSpi implementation for the specified algorithm.
See Also:
Provider

getInstance

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

A new KeyGenerator object encapsulating the KeyGeneratorSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list.

/smscaster-37-key-generator.html. Note that the list of registered providers may be retrieved via the Security.getProviders() method.

Parameters:
algorithm - the standard name of the requested key algorithm. See Appendix A in the Java Cryptography Architecture Reference Guide for information about standard algorithm names.
provider - the name of the provider.
Returns:
the new KeyGenerator object.
Throws:
NullPointerException - if the specified algorithm is null.
NoSuchAlgorithmException - if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified provider.
NoSuchProviderException - if the specified provider is not registered in the security provider list.
IllegalArgumentException - if the provider is null or empty.
See Also:
Provider

getInstance

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

A new KeyGenerator object encapsulating the KeyGeneratorSpi implementation from the specified Provider object is returned. Note that the specified Provider object does not have to be registered in the provider list.

Parameters:
algorithm - the standard name of the requested key algorithm. See Appendix A in the Java Cryptography Architecture Reference Guide for information about standard algorithm names.
provider - the provider.
Returns:
the new KeyGenerator object.
Throws:
NullPointerException - if the specified algorithm is null.
NoSuchAlgorithmException - if a KeyGeneratorSpi implementation for the specified algorithm is not available from the specified Provider object.
IllegalArgumentException - if the provider is null.
See Also:
Provider

getProvider

Returns the provider of this KeyGenerator object.
Returns:
the provider of this KeyGenerator object

init

Initializes this key generator.
Parameters:
random - the source of randomness for this generator

init

Initializes this key generator with the specified parameter set.

If this key generator requires any random bytes, it will get them using the SecureRandom implementation of the highest-priority installed provider as the source of randomness. (If none of the installed providers supply an implementation of SecureRandom, a system-provided source of randomness will be used.)

Parameters:
params - the key generation parameters
Throws:
InvalidAlgorithmParameterException - if the given parameters are inappropriate for this key generator

init

Initializes this key generator with the specified parameter set and a user-provided source of randomness.
Parameters:
params - the key generation parameters
random - the source of randomness for this key generator
Throws:
InvalidAlgorithmParameterException - if params is inappropriate for this key generator

init

Initializes this key generator for a certain keysize.

If this key generator requires any random bytes, it will get them using the SecureRandom implementation of the highest-priority installed provider as the source of randomness. (If none of the installed providers supply an implementation of SecureRandom, a system-provided source of randomness will be used.)

Parameters:
keysize - the keysize. This is an algorithm-specific metric, specified in number of bits.
Throws:
InvalidParameterException - if the keysize is wrong or not supported.

init

Initializes this key generator for a certain keysize, using a user-provided source of randomness.
Parameters:
keysize - the keysize. This is an algorithm-specific metric, specified in number of bits.
random - the source of randomness for this key generator
Throws:
InvalidParameterException - if the keysize is wrong or not supported.

generateKey

Generates a secret key.
Returns:
the new key
OverviewPackageClassUseTreeDeprecatedIndexHelp
Java™ Platform
Standard Ed. 6
FRAMESNO FRAMESAll Classes SUMMARY: NESTED FIELD CONSTR METHODDETAIL: FIELD CONSTR METHODSubmit a bug or feature
For further API reference and developer documentation, see Java SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright © 1993, 2011, Oracle and/or its affiliates. All rights reserved.

The golden rule of Web application security is to never trust data fromuntrusted sources. Sometimes it can be useful to pass data through anuntrusted medium. Cryptographically signed values can be passed through anuntrusted channel safe in the knowledge that any tampering will be detected.

Django provides both a low-level API for signing values and a high-level APIfor setting and reading signed cookies, one of the most common uses ofsigning in Web applications.

You may also find signing useful for the following:

  • Generating “recover my account” URLs for sending to users who havelost their password.
  • Ensuring data stored in hidden form fields has not been tampered with.
  • Generating one-time secret URLs for allowing temporary access to aprotected resource, for example a downloadable file that a user haspaid for.

Protecting the SECRET_KEY

When you create a new Django project using startproject, thesettings.py file is generated automatically and gets a randomSECRET_KEY value. This value is the key to securing signeddata – it is vital you keep this secure, or attackers could use it togenerate their own signed values.

Using the low-level API¶

Django’s signing methods live in the django.core.signing module.To sign a value, first instantiate a Signer instance:

The signature is appended to the end of the string, following the colon.You can retrieve the original value using the unsign method:

If the signature or value have been altered in any way, adjango.core.signing.BadSignature exception will be raised:

By default, the Signer class uses the SECRET_KEY setting togenerate signatures. You can use a different secret by passing it to theSigner constructor:

class Signer(key=None, sep=':', salt=None)[source]

Returns a signer which uses key to generate signatures and sep toseparate values. sep cannot be in the URL safe base64 alphabet. This alphabet containsalphanumeric characters, hyphens, and underscores.

Using the salt argument¶

If you do not wish for every occurrence of a particular string to have the samesignature hash, you can use the optional salt argument to the Signerclass. Using a salt will seed the signing hash function with both the salt andyour SECRET_KEY:

Free Keygens Downloads

Using salt in this way puts the different signatures into differentnamespaces. A signature that comes from one namespace (a particular saltvalue) cannot be used to validate the same plaintext string in a differentnamespace that is using a different salt setting. The result is to prevent anattacker from using a signed string generated in one place in the code as inputto another piece of code that is generating (and verifying) signatures using adifferent salt.

Unlike your SECRET_KEY, your salt argument does not need to staysecret.

Verifying timestamped values¶

TimestampSigner is a subclass of Signer that appends a signedtimestamp to the value. This allows you to confirm that a signed value wascreated within a specified period of time:

class TimestampSigner(key=None, sep=':', salt=None)[source]
sign(value)[source]

Sign value and append current timestamp to it.

Secret Key Generate Java Docs Free

unsign(value, max_age=None)[source]

Checks if value was signed less than max_age seconds ago,otherwise raises SignatureExpired. The max_age parameter canaccept an integer or a datetime.timedelta object.

Protecting complex data structures¶

If you wish to protect a list, tuple or dictionary you can do so using thesigning module’s dumps and loads functions. These imitate Python’spickle module, but use JSON serialization under the hood. JSON ensures thateven if your SECRET_KEY is stolen an attacker will not be ableto execute arbitrary commands by exploiting the pickle format:

Because of the nature of JSON (there is no native distinction between listsand tuples) if you pass in a tuple, you will get a list fromsigning.loads(object):

dumps(obj, key=None, salt='django.core.signing', compress=False)[source]

Returns URL-safe, sha1 signed base64 compressed JSON string. Serializedobject is signed using TimestampSigner.

loads(string, key=None, salt='django.core.signing', max_age=None)[source]

Secret Key Generate Java Docs 2017

Reverse of dumps(), raises BadSignature if signature fails.Checks max_age (in seconds) if given.