-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'import' syntax compatible and TypeScript declaration file #1038
Merged
radetsky
merged 31 commits into
cossacklabs:release/0.15.0
from
radetsky:rad/jsthemis_exports
Nov 28, 2023
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
7cafa69
exports
radetsky abe6207
Add TypeScript declaration file (index.d.ts)
radetsky a1cdad8
Remove non-existent methods
radetsky 473401d
Add example for TypeScript
radetsky 4864b00
Add flow for NodeJS with Typescript example
radetsky dfc73ca
restore test-nodejs.yaml
radetsky ede00cd
add test-nodejs-openssl3.yaml
radetsky c1d4275
update test-nodejs-openssl3.yaml
radetsky c0694b3
update test-nodejs-openssl3.yaml
radetsky 688a7dc
update test-nodejs-openssl3.yaml
radetsky 2bfd34a
update test-nodejs-openssl3.yaml
radetsky b19cc94
update test-nodejs-openssl3.yaml
radetsky 671fc81
update test-nodejs-openssl3.yaml
radetsky 91a1275
remove failed test-nodejs-openssl3.yaml
radetsky a8c0865
move TS examples to another PR
radetsky dcc1182
restore previous test-nodejs.yaml
radetsky f7c1f2d
CHANGELOG.md updated
radetsky 57cc538
added two constructors
radetsky a7f1af8
added import_module.mjs and test
radetsky 5f12c5b
remove not needed string
radetsky 576c69a
Merge pull request #1 from radetsky/rad/import_module_mjs
radetsky 2c35b56
ts tests 1
radetsky 5040c7b
update workflow
radetsky 8fde76e
update workflow
radetsky dddc4ca
update workflow
radetsky 1072e0f
update workflow
radetsky a9d0a5e
downgrade typescript to 4.4 to be compatible with node 12
radetsky 8457223
update workflow
radetsky 3afd6af
update workflow
radetsky a393159
Merge pull request #2 from radetsky/rad/ts_tests
radetsky 1f28271
delete secure_cell.js from ts/
radetsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 @@ | ||
import themis from 'jsthemis'; | ||
const { SymmetricKey, KeyPair} = themis; | ||
|
||
let masterKey = new SymmetricKey() | ||
console.log(masterKey); | ||
|
||
let keypair = new KeyPair() | ||
let privateKey = keypair.private() | ||
let publicKey = keypair.public() | ||
console.log(privateKey); | ||
console.log(publicKey); |
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,35 @@ | ||
# Examples for JsThemis using TypeScript | ||
|
||
## Install | ||
``` | ||
git clone https://github.com/cossacklabs/themis.git | ||
cd docs/examples/ts | ||
npm install | ||
``` | ||
|
||
## Compile | ||
``` | ||
npm run compile | ||
``` | ||
The command will compile TypeScript code into Javascript. | ||
|
||
## Run | ||
``` | ||
node ./secure_keygen.js | ||
node ./secure_cell.js | ||
node ./secure_message.js enc private1 public2 message | ||
node ./secure_message.js dec private2 public1 message | ||
node ./secure_comparator_server.js | ||
node ./secure_comparator_client.js | ||
node ./secure_session_server.js | ||
node ./secure_session_client.js | ||
``` | ||
`private1, private2, public1, public2` is the keys which can be generated by `secure_keygen.js` | ||
|
||
Just run command by command to see how examples work. | ||
|
||
## Clean | ||
``` | ||
npm run clean | ||
``` | ||
The command will delete all compiled JS files. |
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,17 @@ | ||
{ | ||
"name": "ts_examples_themis", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "secure_keygen.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"clean": "rm *.js", | ||
"compile": "find . -maxdepth 1 -type f -name '*.ts' -exec ./node_modules/.bin/tsc {} \\;" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@types/node": "^20.9.5", | ||
"typescript": "^4.4" | ||
} | ||
} |
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,49 @@ | ||
import { SecureCellSeal, SecureCellTokenProtect, SecureCellContextImprint } from "jsthemis" | ||
|
||
const message = Buffer.from('Test Message Please Ignore', 'utf-8') | ||
const context = Buffer.from('Secure Cell example code','utf-8') | ||
const master_key = Buffer.from('bm8sIHRoaXMgaXMgbm90IGEgdmFsaWQgbWFzdGVyIGtleQ==', 'base64') | ||
const passphrase = 'My Litte Secret: Passphrase Is Magic' | ||
|
||
console.log('# Secure Cell in Seal mode\n') | ||
console.log('## Master key API\n') | ||
const scellMK = SecureCellSeal.withKey(master_key) | ||
const encrypted_message = scellMK.encrypt(message) | ||
console.log('Encrypted: ' + Buffer.from(encrypted_message).toString('base64')) | ||
const decrypted_message = scellMK.decrypt(encrypted_message) | ||
console.log('Decrypted: ' + Buffer.from(decrypted_message).toString()) | ||
console.log() | ||
|
||
const encrypted_message2 = Buffer.from('AAEBQAwAAAAQAAAAEQAAAC0fCd2mOIxlDUORXz8+qCKuHCXcDii4bMF8OjOCOqsKEdV4+Ga2xTHPMupFvg==', 'base64') | ||
const decrypted_message2 = scellMK.decrypt(encrypted_message2) | ||
console.log('Decrypted (simulator): ' + Buffer.from(decrypted_message2).toString()) | ||
console.log() | ||
console.log('## Passphrase API\n') | ||
|
||
const scellPW = SecureCellSeal.withPassphrase(passphrase) | ||
const encrypted_message3 = scellPW.encrypt(message) | ||
console.log('Encrypted: ' + Buffer.from(encrypted_message3).toString('base64')) | ||
const decrypted_message3 = scellPW.decrypt(encrypted_message3) | ||
console.log('Decrypted: ' + Buffer.from(decrypted_message3).toString()) | ||
console.log() | ||
|
||
console.log('# Secure Cell in Token Protect mode\n') | ||
|
||
const scellTP = SecureCellTokenProtect.withKey(master_key) | ||
const encrypted_message4 = scellTP.encrypt(message) | ||
console.log('Encrypted: ' + Buffer.from(encrypted_message4.data).toString('base64')) | ||
console.log('Auth token: ' + Buffer.from(encrypted_message4.token).toString('base64')) | ||
const decrypted_message4 = scellTP.decrypt(encrypted_message4.data, encrypted_message4.token) | ||
console.log('Decrypted: ' + Buffer.from(decrypted_message4).toString()) | ||
console.log('') | ||
|
||
console.log('# Secure Cell in Context Imprint mode\n') | ||
const scellCI = SecureCellContextImprint.withKey(master_key) | ||
const encrypted_message5 = scellCI.encrypt(message, context) | ||
console.log('Encrypted: ' + Buffer.from(encrypted_message5).toString('base64')) | ||
const decrypted_message5 = scellCI.decrypt(encrypted_message5, context) | ||
console.log('Decrypted: ' + Buffer.from(decrypted_message5).toString()) | ||
console.log('') | ||
console.log('SecureCell example code finished') | ||
|
||
|
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,24 @@ | ||
import { Socket } from 'net'; | ||
import { SecureComparator } from 'jsthemis'; | ||
|
||
const comparator = new SecureComparator(Buffer.from("secret")); | ||
|
||
const client = new Socket(); | ||
client.connect(1337, '127.0.0.1', () => { | ||
console.log('Connected'); | ||
client.write(comparator.beginCompare()); | ||
}); | ||
|
||
client.on('data', (data: Buffer) => { | ||
const d = comparator.proceedCompare(data); | ||
if (!comparator.isCompareComplete()) { | ||
client.write(d); | ||
} else { | ||
console.log(comparator.isMatch()); | ||
client.destroy(); | ||
} | ||
}); | ||
|
||
client.on('close', () => { | ||
console.log('Connection closed'); | ||
}); |
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,17 @@ | ||
import { createServer, Socket } from 'net'; | ||
import { SecureComparator } from 'jsthemis'; | ||
|
||
const server = createServer((socket: Socket) => { | ||
const comparator = new SecureComparator(Buffer.from("secret")); | ||
|
||
socket.on('data', (data: Buffer) => { | ||
const d = comparator.proceedCompare(data); | ||
socket.write(d); | ||
if (comparator.isCompareComplete()) { | ||
console.log(comparator.isMatch()); | ||
socket.destroy(); | ||
} | ||
}); | ||
}); | ||
|
||
server.listen(1337, '127.0.0.1'); |
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,8 @@ | ||
import { KeyPair, SymmetricKey } from 'jsthemis'; | ||
|
||
const masterKey = new SymmetricKey() as Uint8Array; | ||
console.log("master key: ", Buffer.from(masterKey).toString("base64")); | ||
|
||
const keypair = new KeyPair(); | ||
console.log("private key: ", Buffer.from(keypair.private()).toString("base64")); | ||
console.log("public key : ", Buffer.from(keypair.public()).toString("base64")); |
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,22 @@ | ||
import { SecureMessage } from 'jsthemis'; | ||
|
||
if (process.argv.length == 6) { | ||
const command = process.argv[2]; | ||
const private_key = process.argv[3]; | ||
const peer_public_key = process.argv[4]; | ||
const secure_message = new SecureMessage( | ||
Buffer.from(private_key, "base64"), | ||
Buffer.from(peer_public_key, "base64")); | ||
|
||
if (command == "enc") { | ||
const encrypted_message = secure_message.encrypt(Buffer.from(process.argv[5])); | ||
console.log(Buffer.from(encrypted_message).toString("base64")); | ||
} else if (command == "dec") { | ||
const decrypted_message = secure_message.decrypt(Buffer.from(process.argv[5], "base64")); | ||
console.log(Buffer.from(decrypted_message).toString("utf8")); | ||
} else { | ||
console.log("usage node secure_message.js <enc/dec> <private key> <peer public key> <message>"); | ||
} | ||
} else { | ||
console.log("usage node secure_message.js <enc/dec> <private key> <peer public key> <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,43 @@ | ||
import { Socket } from 'net'; | ||
import { SecureSession } from 'jsthemis'; | ||
|
||
const session = new SecureSession( | ||
Buffer.from("client"), | ||
Buffer.from("UkVDMgAAAC3DZR2qAEbvO092R/IKXBttnf9dVSU65R+Fb4eNoyxxlzn2n4GR", "base64"), | ||
(id: Uint8Array) => { | ||
if (id.toString() === "server") { | ||
return Buffer.from("VUVDMgAAAC30/vs+AwciK6egi82A9TkTydVuOzMFsJ9AkA0gMGyNH0tSu5Bk", "base64"); | ||
} else if (id.toString() === "client") { | ||
return Buffer.from("VUVDMgAAAC15KNjgAr1DQEw+So1oztUarO4Jw/CGgyehBRCbOxbpHrPBKO7s", "base64"); | ||
} | ||
return null; | ||
} | ||
); | ||
|
||
let retry_count = 5; | ||
|
||
const client: Socket = new Socket(); | ||
client.connect(1337, '127.0.0.1', () => { | ||
console.log('Connected'); | ||
client.write(session.connectRequest()); | ||
}); | ||
|
||
client.on('data', (data: Buffer) => { | ||
const d = session.unwrap(data); | ||
if (!session.isEstablished()) { | ||
client.write(d); | ||
} else { | ||
if (d !== undefined) { | ||
console.log(d.toString()); | ||
} | ||
if (retry_count--) { | ||
client.write(session.wrap(Buffer.from("Hello server!!!"))); | ||
} else { | ||
client.destroy(); | ||
} | ||
} | ||
}); | ||
|
||
client.on('close', () => { | ||
console.log('Connection closed'); | ||
}); |
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,30 @@ | ||
import { createServer, Socket } from 'net'; | ||
import { SecureSession } from 'jsthemis'; | ||
|
||
const server = createServer((socket: Socket) => { | ||
const session = new SecureSession( | ||
Buffer.from("server"), | ||
Buffer.from("UkVDMgAAAC0U6AK7AAm6ha0cgHmovSTpZax01+icg9xwFlZAqqGWeGTqbHUt", "base64"), | ||
(id: Uint8Array) => { | ||
if (id.toString() === "server") { | ||
return Buffer.from("VUVDMgAAAC30/vs+AwciK6egi82A9TkTydVuOzMFsJ9AkA0gMGyNH0tSu5Bk", "base64"); | ||
} else if (id.toString() === "client") { | ||
return Buffer.from("VUVDMgAAAC15KNjgAr1DQEw+So1oztUarO4Jw/CGgyehBRCbOxbpHrPBKO7s", "base64"); | ||
} | ||
return null; | ||
} | ||
); | ||
|
||
socket.on('data', (data: Buffer) => { | ||
if (!session.isEstablished()) { | ||
const d = session.unwrap(data); | ||
socket.write(d); | ||
} else { | ||
const d = session.unwrap(data); | ||
console.log(d.toString()); | ||
socket.write(session.wrap(d)); | ||
} | ||
}); | ||
}); | ||
|
||
server.listen(1337, '127.0.0.1'); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to be sure, did you tried some failed script to check some failure case to be sure that it's not always
echo ok
?I found documentation for run where described that by default
shell
used with-e
flag. But real world can be unsynchronized from documentation and we met situations when tests ran but didn't exit with errors on failures.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.