owned this note
owned this note
Published
Linked with GitHub
# Cryptography
**Cryptography** is the science of protecting information by transforming it into a secure format. It ensures that communications are kept confidential and are protected from unauthorized access.
## Hash
**Hashing** transforms specific data into a hash value using a chosen **hashing algorithm**. This method is particularly useful for securely storing sensitive information, such as passwords, or for verifying data integrity.
Hashing is a one-way process, meaning that the original data cannot be reconstructed from the hash value.
> **quick.cryptography.hash(data: IHashDataRequest): Promise<IHashDataResponse | undefined>**
>**IHashDataRequest** {
data: string,
algorithm: HashAlgorithm,
key: string
}
>> **data** : The data to be hashed<br/>
>> **algorithm** : The hashing algorithm to use<br/>
>> **key** : The hashing key to use<br/>
>> ***Note** : RSA uses a public key for encryption and a private key for decryption and signing. HMAC uses a secret key to verify message integrity and authenticity.***
>>HashAlgorithm {
>> SHA1,
>> SHA256,
>> SHA384,
>> SHA512,
>> HMACSHA1,
>> HMACSHA256,
HMACSHA384,
HMACSHA512,
>>}
>**IHashDataResponse** {
hashedData: string
}
**Here you can see an example:**
```js
async function createHash() {
let plaintext = 'This text will be encoded UTF8 and may contain special characters like § and €.';
let hashRequest: IHashDataRequest = {
algorithm: HashAlgorithm.SHA512,
data: plaintext
}
let hashResponse = await quick.cryptography.hash(hashRequest);
}
createHash();
```
## Encrypt
Encrypts a specific data using the specified encryption algorithm. This process ensures that the data is safely stored or transmitted. The data is encrypted using an encryption key and can only be decrypted with the correct key.
> **quick.cryptography.encrypt(data: IEncryptDataRequest): Promise<IEncryptDataResponse | undefined>**
>**IEncryptDataRequest** {
data: string,
key: string,
algorithm: EncryptionAlgorithm
}
>> **data** : The data to be encrypted<br/>
>> **key** : The encryption key<br/>
>> **algorithm** : Specifies which encryption algorithm to use.<br/>
>> EncryptionAlgorithm {
RSAOAEP,
AESCTR,
AESCBC,
AESGCM
}
>**IEncryptDataResponse** {
encryptedData: string,
iv?: string
}
>> **encryptedData** : Holds the encrypted data<br/>
>> **iv (optional)** : Represents the initialization vector used in encryption. ( If algorithm is "RSAOAEP", then iv is not used, otherwise it is required. )
**Here you can see an example:**
```js
async function encryptData() {
let plaintext = 'This text will be encoded UTF8 and may contain special characters like § and €.';
let requestEncrypt: IEncryptDataRequest = {
algorithm: EncryptionAlgorithm.AESGCM,
data: plaintext,
key: "secretKey"
}
let responseEncrypt = await quick.cryptography.encrypt(requestEncrypt);
}
encryptData();
```
## Decrypt
Decryption is the process of converting encrypted data back to its original, readable form. Decryption uses the correct key to revert the encrypted data back to its original form. It also ensures that authorized users can access and understand the protected information.
> **quick.cryptography.decrypt(data: IDecryptDataRequest): Promise<IDecryptDataResponse | undefined>**
> **IDecryptDataRequest** {
encryptedData: string,
iv?: string,
key: string,
algorithm: EncryptionAlgorithm
}
>> **encryptedData** : The encrypted data<br/>
>> **iv (optional)** : The initialization vector used during encryption. ( If algorithm is "RSAOAEP", then iv is not used, otherwise it is required. )<br/>
>> **key** : The encryption key<br/>
>> **algorithm** : Specifies which encryption algorithm to use<br/>
>> EncryptionAlgorithm {
RSAOAEP,
AESCTR,
AESCBC,
AESGCM
}
> **IDecryptDataResponse** {
decryptedData: string
}
>> **decryptedData** : Contains the original data decrypted from the encrypted data.
**Here you can see an example:**
```js
async function decryptData(encryptedData, iv) {
let requestDecrypt: IDecryptDataRequest = {
algorithm: EncryptionAlgorithm.AESGCM,
encryptedData: encryptedData,
key: "secretKey",
iv: iv
}
let responseDecrypt = await quick.cryptography.decrypt(requestDecrypt);
}
decryptData();
```
## Samples
<a href="https://studio.onplateau.com/standAlone/docsEditor/crypto" target="_blank">cryptographySample</a>