Kaven-Basic
Installing
npm install kaven-basic
yarn add kaven-basic
pnpm i kaven-basic
Usage
Node.js:
import { GenerateGuid } from "kaven-basic";
console.log(GenerateGuid());
Browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="kaven-basic/bundles/index.umd.js"></script>
</head>
<body>
<script>alert(KavenBasic.GenerateGuid());</script>
<script>
(async () => {
try {
const KavenBasic = window.KavenBasic;
const {
KavenLog,
FormatCurrentDate,
GenerateGuid,
KavenBrowserRSA,
SequenceEqual,
AnsiColors_BrightMagenta,
AnsiColors_Green,
} = KavenBasic;
{
KavenLog.CreateInfo(FormatCurrentDate()).WriteToConsole({
Level: AnsiColors_BrightMagenta,
Message: AnsiColors_Green,
});
KavenLog.CreateWarn(GenerateGuid()).WriteToConsole();
}
const str = "test 中文";
{
const rsa = await KavenBrowserRSA.Create();
console.log(rsa.PublicKey);
console.log(rsa.PrivateKey);
const enc = new TextEncoder();
const data = enc.encode(str);
const encryptedData = await rsa.Encrypt(data);
const decryptedData = await rsa.Decrypt(encryptedData);
console.log(SequenceEqual(new Uint8Array(decryptedData), data));
const dec = new TextDecoder();
const decryptedStr = dec.decode(decryptedData);
console.log(str === decryptedStr);
const signature = await rsa.Sign(data);
const pass = await rsa.Verify(data, signature);
console.log(pass);
}
{
const publicKey = `-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEArb0e2QEoDsnL41+3esUo1WUp6/lxYwhJd9CvAyahXlZNjLnLOpSK2VjTaGMU+FiL4pwCYpQDAjDl8dPyGokVBwG34e6i4nOmGl0cHnuE+fwPZrP7cQwVMVYHHRpVaVwDLHls3Jmz8xOlvihPEyBxzBYXD5Ssus227Akl0FRhaJ3P+va7vENMGILEIjKTF9HAafHxYWay4qj6fiwyS+mx8DoNVsYaDtcRrgqm2HOiyPST3qBnk9IH171Ozo7bUzJsvQrtBUVnuuYR8MlcbamMQxdlG6sC9fZRX4JCiF2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AQAD7NaIw/XBdlN6RCrQ+BrWsqyIjKXj7l+/L00GyQ4UFkWULPgVoE6zya86s9SJRv2OtTrgXQCms/XM7pNV+U4fnh1ioBhb0XPjADyz8nye7IAJswfhthdyFa3QVH6ryLi85TKIstf1RHGnpbtaIrVslamD5DmIrfA4D+knjMV3AcvRlid7fNXCGW3zDAna2BuW5w5NPTCgYrtcIlilmsZC379cRn2sR+UTB42OQLVXp3q2guKvYO8iZQp9yrdwJ0abjzEFBsayBS9OHlKZdIKdForhZp4GTng5FeJnJyp/dILT49LhEo2BRR/uyezXOZgiwpoF2gZi4kk5rJ8zGQMc
-----END PRIVATE KEY-----`;
const str = "kaven@wuwenkai.com 中文";
const rsa = new KavenBrowserRSA(publicKey, privateKey);
console.assert(await rsa.DecryptFromString(await rsa.EncryptToString(str)) === str);
console.assert(await rsa.VerifyFromString(str, await rsa.SignToString(str)));
const encryptedData = "HzAXSygyMgIMNr5TlCyL/uYdSjFjL1V0Ccioq85KO5Sx9a6IvTKSZQkVDtNYron4mwcj/n7ZaATpSJDn3cJWHihtTYNiQeMS960WHEaCYtyipx4k43BJoxlRkUy0SDpdPnsQ032V7emDIB+L2x/Ju01xDbF0z5igSTIXJFbtS2epD4kkrmMdHLc/d2WdnBgK5qFJOArehStC007CQN8XsnXTxgFMMYO7emIqGLMCvzrfHBCjN380Ns3yYcOqyws07SeOAxbM7EYG5B9zyJQXdSG00h8GWKDlB+/PqGY0pPuKwwMfRLns5sn1twCM6MVqShvsS6E3yiFWsbZS4NJy4hBPRiQirslpp3XOB0OEEByvQ58dtuFo/bGwyfI9FsNymqof6Kp5Y9SUYZwbCgxCvI0mqAyHdD10uWxi7lm1klazywb5roCzWqMgMhqa8+6XmcyDKZSKWuVu70vV8WZvKLTnOi6TmdSe9xz/5Vo6OoAAj1YPMh+7GMu1HLruthl2/TGfdzwH7F4tkrcaWLZVZ3ZpKP/SXg38NnMlg3L/B3i4gU57XVGSkYPTLvN3zFx4K7/Zld7Y6TZzt7p2qlUt475jBE8eZ5U1Fllwf7V5al2yAZkm3HHq8WUeVdabOq0FySd1GLyy0+Fgqp+kpBoCtRZN47ICxFD+KR/HsYBIJVw=";
const decryptedStr = await rsa.DecryptFromString(encryptedData);
console.assert(decryptedStr === str);
const signature = "QF0gO0tbCUMa5RLm3pY7UoQiR07vMFPBLf/Gt9hcD2HP5wcBDE3/ttgy3WQxoxscW46zy1alR/tNMT+OPvn8W+IU1IJT+oRORsiQAMvWev8UHaF7b5oU0+Oe6Ez0pcR9dSWwziaiGLtQ1yFFwzyivXO/iH+RIAXu5l9cOMIl+ah94d5ZgNd49I+ZnmLbAuzwD0dOiV/bNSB+3+iMgnqOqu9FURqInZZtoWM+rV5mZ7T/QOYGgyUZkpc5hszD7wztBXjNqp/yyIuCnGkSoc34Vu3lcSEOqik3S6caWFv0iJoy9smyo7x3uOcTaQ4+OfGFdJzSQA1DwVZUlfxlmzMpp/cRjO4b5lBmcwzLxoEKJtvhagc+n51bT63/IW1pFhpwf9deZ+moSIbZfAZn2Ytb7eij+zuXe/Jqs2cGLu/rcnhMJEf7/+sCzuxUtd/xBWx7gGTKGjPrgnlw23eSwfnUsL+HvicHBTgyXrOI8lofmMqsT5shnjQGgMpCECs3q9rhq258mLB0syTqtA8Gg9CDz+s1aketjNqLcdaxDCqNM0LYOEvY7kL9AYrmjqyTg5Ke2TdJx5pdXyqbjBSGEWY7FXb+LZhCLQpCdu3SFQ0isV3L7z+zvwEOlIfL8aHqjfKZHxW+NSUiRBXYOCfy8Xe5ZBKLMuks0iQSwGi3mCjGbrU=";
console.assert(await rsa.VerifyFromString(str, signature));
}
} catch (ex) {
console.error(ex);
}
})();
</script>
</body>
</html>
Documentation
Kaven-Basic