Partner Integration Docs
  • Getting Started
  • Architecture Overview
  • Survey Wall Integration
  • UAT Test
  • Troubleshooting
  • Code Snippet
    • Golang
    • Nodejs
    • Flutter
    • Java / Kotlin
Powered by GitBook
On this page
  • Hash Email and Phone Number
  • Encrypt Signed body
  1. Code Snippet

Nodejs

Nodejs Code Snippet

Hash Email and Phone Number

Dependency:

  • const crypto = require('crypto');: This imports the built-in crypto module in Node.js, which provides cryptographic functionality.

const crypto = require('crypto');

function hashSHA256(input) {
    const hash = crypto.createHash('sha256');
    hash.update(input);
    return hash.digest('hex');
}

// Example usage
const email = 'example@example.com';
const phone = '+1234567890';

// Hash email
const emailHash = hashSHA256(email);
console.log('Hashed email:', emailHash);

// Hash phone number
const phoneHash = hashSHA256(phone);
console.log('Hashed phone number:', phoneHash);

Encrypt Signed body

Dependency:

crypto: This built-in Node.js module provides cryptographic functionalities including hashing, encryption, and decryption.

const crypto = require('crypto');

function main() {
    const partnerId = "YOUR-VALID-PARTNER-ID";
    const partnerSecret = "8d8dd8af95971d949f4c6a941bc86696";

    const emailHash = hashSHA256("user@email.com");
    const phoneHash = hashSHA256("085123456789");

    const payload = {
        partnerId,
        emailHash,
        phoneHash
    };

    try {
        const jsonString = JSON.stringify(payload);
        const encryptedPayload = encrypt(jsonString, partnerSecret);
        console.log(encryptedPayload);
    } catch (error) {
        console.error('Encryption error:', error);
    }
}

function encrypt(str, secret) {
    const key = Buffer.from(secret, 'hex');
    
    const cipher = crypto.createCipheriv('aes-128-cfb', key, key);
    let encrypted = cipher.update(str, 'utf8', 'base64');
    encrypted += cipher.final('base64');

    return encrypted;
}

main();
PreviousGolangNextFlutter

Last updated 10 months ago