How to use the buffer-xor/inplace function in buffer-xor

To help you get started, we’ve selected a few buffer-xor examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ArkEcosystem / core / packages / crypto / src / crypto / bip38.ts View on Github external
const derivedHalf2 = seedBPass.slice(32, 64);

    const decipher = aes.createDecipheriv("aes-256-ecb", derivedHalf2, Buffer.alloc(0));
    decipher.setAutoPadding(false);
    decipher.end(encryptedPart2);

    const decryptedPart2 = decipher.read();
    const tmp = xor(decryptedPart2, derivedHalf1.slice(16, 32));
    const seedBPart2 = tmp.slice(8, 16);

    const decipher2 = aes.createDecipheriv("aes-256-ecb", derivedHalf2, Buffer.alloc(0));
    decipher2.setAutoPadding(false);
    decipher2.write(encryptedPart1); // first 8 bytes
    decipher2.end(tmp.slice(0, 8)); // last 8 bytes

    const seedBPart1 = xor(decipher2.read(), derivedHalf1.slice(0, 16));
    const seedB = Buffer.concat([seedBPart1, seedBPart2], 24);
    const privateKey = secp256k1.privateKeyTweakMul(HashAlgorithms.hash256(seedB), passFactor);

    return {
        privateKey,
        compressed,
    };
};
github ArkEcosystem / core / packages / crypto / src / crypto / bip38.ts View on Github external
const encryptRaw = (buffer: Buffer, compressed: boolean, passphrase: string): Buffer => {
    if (buffer.length !== 32) {
        throw new PrivateKeyLengthError(32, buffer.length);
    }

    const address = getAddressPrivate(buffer, compressed);

    const secret = Buffer.from(passphrase, "utf8");
    const salt = HashAlgorithms.hash256(address).slice(0, 4);

    const scryptBuf = crypto.scryptSync(secret, salt, 64, SCRYPT_PARAMS);
    const derivedHalf1 = scryptBuf.slice(0, 32);
    const derivedHalf2 = scryptBuf.slice(32, 64);

    const xorBuf = xor(derivedHalf1, buffer);
    const cipher = aes.createCipheriv("aes-256-ecb", derivedHalf2, NULL);
    cipher.setAutoPadding(false);
    cipher.end(xorBuf);

    const cipherText = cipher.read();

    // 0x01 | 0x42 | flagByte | salt (4) | cipherText (32)
    const result = Buffer.allocUnsafe(7 + 32);
    result.writeUInt8(0x01, 0);
    result.writeUInt8(0x42, 1);
    result.writeUInt8(compressed ? 0xe0 : 0xc0, 2);
    salt.copy(result, 3);
    cipherText.copy(result, 7);

    return result;
};
github ArkEcosystem / core / packages / crypto / src / crypto / bip38.ts View on Github external
if (!compressed && flagByte !== 0xc0) {
        throw new Bip38CompressionError(0xc0, flagByte);
    }

    const salt = buffer.slice(3, 7);
    const scryptBuf = crypto.scryptSync(passphrase, salt, 64, SCRYPT_PARAMS);
    const derivedHalf1 = scryptBuf.slice(0, 32);
    const derivedHalf2 = scryptBuf.slice(32, 64);

    const privKeyBuf = buffer.slice(7, 7 + 32);
    const decipher = aes.createDecipheriv("aes-256-ecb", derivedHalf2, NULL);
    decipher.setAutoPadding(false);
    decipher.end(privKeyBuf);

    const plainText = decipher.read();
    const privateKey = xor(derivedHalf1, plainText);

    // verify salt matches address
    const address = getAddressPrivate(privateKey, compressed);

    const checksum = HashAlgorithms.hash256(address).slice(0, 4);
    assert.deepEqual(salt, checksum);

    return {
        privateKey,
        compressed,
    };
};
github ArkEcosystem / core / packages / crypto / src / crypto / bip38.ts View on Github external
const publicKey = getPublicKey(passFactor, true);
    const seedBPass = crypto.scryptSync(publicKey, Buffer.concat([addressHash, ownerEntropy]), 64, {
        N: 1024,
        r: 1,
        p: 1,
    });
    const derivedHalf1 = seedBPass.slice(0, 32);
    const derivedHalf2 = seedBPass.slice(32, 64);

    const decipher = aes.createDecipheriv("aes-256-ecb", derivedHalf2, Buffer.alloc(0));
    decipher.setAutoPadding(false);
    decipher.end(encryptedPart2);

    const decryptedPart2 = decipher.read();
    const tmp = xor(decryptedPart2, derivedHalf1.slice(16, 32));
    const seedBPart2 = tmp.slice(8, 16);

    const decipher2 = aes.createDecipheriv("aes-256-ecb", derivedHalf2, Buffer.alloc(0));
    decipher2.setAutoPadding(false);
    decipher2.write(encryptedPart1); // first 8 bytes
    decipher2.end(tmp.slice(0, 8)); // last 8 bytes

    const seedBPart1 = xor(decipher2.read(), derivedHalf1.slice(0, 16));
    const seedB = Buffer.concat([seedBPart1, seedBPart2], 24);
    const privateKey = secp256k1.privateKeyTweakMul(HashAlgorithms.hash256(seedB), passFactor);

    return {
        privateKey,
        compressed,
    };
};

buffer-xor

A simple module for bitwise-xor on buffers

MIT
Latest version published 7 years ago

Package Health Score

65 / 100
Full package analysis

Popular buffer-xor functions