-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Encrypting private keys with SecretBox
- Loading branch information
1 parent
306389b
commit b8cbdd4
Showing
14 changed files
with
167 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { SecretBox } from 'daf-libsodium' | ||
import program from 'commander' | ||
|
||
program | ||
.command('crypto') | ||
.option('-s, --secret', 'Generate secret key') | ||
.description('Crypto') | ||
.action(async raw => { | ||
try { | ||
const secretKey = await SecretBox.createSecretKey() | ||
console.log(secretKey) | ||
} catch (e) { | ||
console.error(e.message) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const fs = require('fs') | ||
import { SecretBox } from 'daf-libsodium' | ||
|
||
const defaultPath = process.env.HOME + '/.daf/' | ||
const envFile = defaultPath + '.env' | ||
|
||
async function main() { | ||
|
||
if (!fs.existsSync(defaultPath)) { | ||
fs.mkdirSync(defaultPath) | ||
} | ||
|
||
if (!fs.existsSync(envFile)) { | ||
console.log('Configuration file does not exist. Creating: ' + envFile) | ||
let env = 'DAF_DATA_STORE=' + defaultPath + 'database-v3.sqlite3' | ||
env += '\nDEBUG_DAF_DB=0' | ||
env += '\nDAF_SECRET_KEY=' + await SecretBox.createSecretKey() | ||
env += '\nDAF_INFURA_ID=5ffc47f65c4042ce847ef66a3fa70d4c' | ||
env += '\n#DEBUG=daf:*' | ||
|
||
fs.writeFileSync(envFile, env) | ||
} | ||
|
||
} | ||
|
||
main().catch(console.error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export abstract class AbstractSecretBox { | ||
abstract encrypt(message: string): Promise<string> | ||
abstract decrypt(encryptedMessageHex: string): Promise<string> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { SecretBox } from '../secret-box' | ||
|
||
describe('daf-libsodium', () => { | ||
|
||
it('should encrypt and decrypt', async () => { | ||
const secretKey = await SecretBox.createSecretKey() | ||
const box = new SecretBox(secretKey) | ||
const message = 'hello world' | ||
|
||
const encrypted = await box.encrypt(message) | ||
const decrypted = await box.decrypt(encrypted) | ||
expect(decrypted).toEqual(message) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { KeyManagementSystem } from './key-management-system' | ||
export { SecretBox } from './secret-box' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { AbstractSecretBox } from 'daf-core' | ||
import sodium from 'libsodium-wrappers' | ||
|
||
export class SecretBox extends AbstractSecretBox { | ||
constructor(private secretKey: string) { | ||
super() | ||
if (!secretKey) { | ||
throw Error('Secret key is required') | ||
} | ||
} | ||
|
||
static async createSecretKey(): Promise<string> { | ||
await sodium.ready | ||
const boxKeyPair = sodium.crypto_box_keypair() | ||
const secretKey = sodium.to_hex(boxKeyPair.privateKey) | ||
return secretKey | ||
} | ||
|
||
async encrypt(message: string): Promise<string> { | ||
await sodium.ready | ||
const nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES) | ||
const cipherText = sodium.crypto_secretbox_easy(message, nonce, sodium.from_hex(this.secretKey)) | ||
return sodium.to_hex(new Uint8Array([ ...nonce, ...cipherText ])) | ||
} | ||
|
||
async decrypt(encryptedMessageHex: string): Promise<string> { | ||
await sodium.ready | ||
const cipherTextWithNonce = sodium.from_hex(encryptedMessageHex) | ||
const nonce = cipherTextWithNonce.slice(0, sodium.crypto_secretbox_NONCEBYTES) | ||
const cipherText = cipherTextWithNonce.slice(sodium.crypto_secretbox_NONCEBYTES) | ||
return sodium.to_string(sodium.crypto_secretbox_open_easy( | ||
cipherText, | ||
nonce, | ||
sodium.from_hex(this.secretKey) | ||
)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { KeyManagementSystem } from './key-management-system' | ||
export { SecretBox } from './secret-box' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { AbstractSecretBox } from 'daf-core' | ||
import sodium from 'react-native-sodium' | ||
|
||
export class SecretBox extends AbstractSecretBox { | ||
constructor(private secretKey: string) { | ||
super() | ||
if (!secretKey) { | ||
throw Error('Secret key is required') | ||
} | ||
} | ||
|
||
static async createSecretKey(): Promise<string> { | ||
await sodium.ready | ||
const boxKeyPair = sodium.crypto_box_keypair() | ||
const secretKey = sodium.to_hex(boxKeyPair.privateKey) | ||
return secretKey | ||
} | ||
|
||
async encrypt(message: string): Promise<string> { | ||
await sodium.ready | ||
const nonce = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES) | ||
const cipherText = sodium.crypto_secretbox_easy(message, nonce, sodium.from_hex(this.secretKey)) | ||
return sodium.to_hex(new Uint8Array([ ...nonce, ...cipherText ])) | ||
} | ||
|
||
async decrypt(encryptedMessageHex: string): Promise<string> { | ||
await sodium.ready | ||
const cipherTextWithNonce = sodium.from_hex(encryptedMessageHex) | ||
const nonce = cipherTextWithNonce.slice(0, sodium.crypto_secretbox_NONCEBYTES) | ||
const cipherText = cipherTextWithNonce.slice(sodium.crypto_secretbox_NONCEBYTES) | ||
return sodium.to_string(sodium.crypto_secretbox_open_easy( | ||
cipherText, | ||
nonce, | ||
sodium.from_hex(this.secretKey) | ||
)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters