mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2024-03-22 13:10:41 +08:00
Encode key as base58
This commit is contained in:
parent
0e18b5d0c1
commit
4aab3c0061
148
js/base-x-3.0.5.1.js
Normal file
148
js/base-x-3.0.5.1.js
Normal file
|
@ -0,0 +1,148 @@
|
|||
// base-x encoding / decoding
|
||||
// based on https://github.com/cryptocoinjs/base-x 3.0.5
|
||||
// modification: removed Buffer dependency and node.modules entry
|
||||
// Copyright (c) 2018 base-x contributors
|
||||
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
var baseX = function base (ALPHABET) {
|
||||
if (ALPHABET.length >= 255) throw new TypeError('Alphabet too long')
|
||||
|
||||
const BASE_MAP = new Uint8Array(256)
|
||||
BASE_MAP.fill(255)
|
||||
|
||||
for (let i = 0; i < ALPHABET.length; i++) {
|
||||
const x = ALPHABET.charAt(i)
|
||||
const xc = x.charCodeAt(0)
|
||||
|
||||
if (BASE_MAP[xc] !== 255) throw new TypeError(x + ' is ambiguous')
|
||||
BASE_MAP[xc] = i
|
||||
}
|
||||
|
||||
const BASE = ALPHABET.length
|
||||
const LEADER = ALPHABET.charAt(0)
|
||||
const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
|
||||
const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
|
||||
|
||||
function encode (source) {
|
||||
if (source.length === 0) return ''
|
||||
|
||||
// Skip & count leading zeroes.
|
||||
let zeroes = 0
|
||||
let length = 0
|
||||
let pbegin = 0
|
||||
const pend = source.length
|
||||
|
||||
while (pbegin !== pend && source[pbegin] === 0) {
|
||||
pbegin++
|
||||
zeroes++
|
||||
}
|
||||
|
||||
// Allocate enough space in big-endian base58 representation.
|
||||
const size = ((pend - pbegin) * iFACTOR + 1) >>> 0
|
||||
const b58 = new Uint8Array(size)
|
||||
|
||||
// Process the bytes.
|
||||
while (pbegin !== pend) {
|
||||
let carry = source[pbegin]
|
||||
|
||||
// Apply "b58 = b58 * 256 + ch".
|
||||
let i = 0
|
||||
for (let it = size - 1; (carry !== 0 || i < length) && (it !== -1); it--, i++) {
|
||||
carry += (256 * b58[it]) >>> 0
|
||||
b58[it] = (carry % BASE) >>> 0
|
||||
carry = (carry / BASE) >>> 0
|
||||
}
|
||||
|
||||
if (carry !== 0) throw new Error('Non-zero carry')
|
||||
length = i
|
||||
pbegin++
|
||||
}
|
||||
|
||||
// Skip leading zeroes in base58 result.
|
||||
let it = size - length
|
||||
while (it !== size && b58[it] === 0) {
|
||||
it++
|
||||
}
|
||||
|
||||
// Translate the result into a string.
|
||||
let str = LEADER.repeat(zeroes)
|
||||
for (; it < size; ++it) str += ALPHABET.charAt(b58[it])
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
function decodeUnsafe (source) {
|
||||
if (typeof source !== 'string') throw new TypeError('Expected String')
|
||||
if (source.length === 0) return ''
|
||||
|
||||
let psz = 0
|
||||
|
||||
// Skip leading spaces.
|
||||
if (source[psz] === ' ') return
|
||||
|
||||
// Skip and count leading '1's.
|
||||
let zeroes = 0
|
||||
let length = 0
|
||||
while (source[psz] === LEADER) {
|
||||
zeroes++
|
||||
psz++
|
||||
}
|
||||
|
||||
// Allocate enough space in big-endian base256 representation.
|
||||
const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
|
||||
const b256 = new Uint8Array(size)
|
||||
|
||||
// Process the characters.
|
||||
while (source[psz]) {
|
||||
// Decode character
|
||||
let carry = BASE_MAP[source.charCodeAt(psz)]
|
||||
|
||||
// Invalid character
|
||||
if (carry === 255) return
|
||||
|
||||
let i = 0
|
||||
for (let it = size - 1; (carry !== 0 || i < length) && (it !== -1); it--, i++) {
|
||||
carry += (BASE * b256[it]) >>> 0
|
||||
b256[it] = (carry % 256) >>> 0
|
||||
carry = (carry / 256) >>> 0
|
||||
}
|
||||
|
||||
if (carry !== 0) throw new Error('Non-zero carry')
|
||||
length = i
|
||||
psz++
|
||||
}
|
||||
|
||||
// Skip trailing spaces.
|
||||
if (source[psz] === ' ') return
|
||||
|
||||
// Skip leading zeroes in b256.
|
||||
let it = size - length
|
||||
while (it !== size && b256[it] === 0) {
|
||||
it++
|
||||
}
|
||||
|
||||
var vch = [];
|
||||
|
||||
let j = zeroes
|
||||
while (it !== size) {
|
||||
vch[j++] = b256[it++]
|
||||
}
|
||||
|
||||
return vch
|
||||
}
|
||||
|
||||
function decode (string) {
|
||||
const buffer = decodeUnsafe(string)
|
||||
if (buffer) return buffer
|
||||
|
||||
throw new Error('Non-base' + BASE + ' character')
|
||||
}
|
||||
|
||||
return {
|
||||
encode: encode,
|
||||
decodeUnsafe: decodeUnsafe,
|
||||
decode: decode
|
||||
}
|
||||
}
|
|
@ -643,7 +643,8 @@ jQuery.PrivateBin = (function($, sjcl, Base64, RawDeflate) {
|
|||
*/
|
||||
me.getSymmetricKey = function()
|
||||
{
|
||||
return sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 10), 0);
|
||||
var bs58 = new baseX('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');
|
||||
return bs58.encode(sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 10), 0));
|
||||
};
|
||||
|
||||
return me;
|
||||
|
|
|
@ -60,6 +60,7 @@ else:
|
|||
<?php
|
||||
endif;
|
||||
?>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/base-x-3.0.5.1.js" integrity="sha512-tZCra9cpyEtRlLCuglgHQ1pZyX1yZ09NYTTUawN/ptrXkK5I2ndKCWdrswIGGq4fYb2LVcodw3lEzT1UiHlCEg==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/rawdeflate-0.5.js" integrity="sha512-tTdZ7qMr7tt5VQy4iCHu6/aGB12eRwbUy+AEI5rXntfsjcRfBeeqJloMsBU9FrGk1bIYLiuND/FhU42LO1bi0g==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/rawinflate-0.3.js" integrity="sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/bootstrap-3.3.7.js" integrity="sha512-iztkobsvnjKfAtTNdHkGVjAYTrrtlC7mGp/54c40wowO7LhURYl3gVzzcEqGl/qKXQltJ2HwMrdLcNUdo+N/RQ==" crossorigin="anonymous"></script>
|
||||
|
@ -76,7 +77,7 @@ if ($MARKDOWN):
|
|||
endif;
|
||||
?>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/purify-1.0.7.js" integrity="sha512-VnKJHLosO8z2ojNvWk9BEKYqnhZyWK9rM90FgZUUEp/PRnUqR5OLLKE0a3BkVmn7YgB7LXRrjHgFHQYKd6DAIA==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-tRRKkyV2RYlU1b/PdUmLigAV8mAQpklblVO071AcQs5QbeUwBnK2L5zjbscm2+B2eRJ06luEisXuPowvFHN6fA==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-zkRNN1TQ1h2agLdyIpjZ7ALamBxeSbmSA1eDUhEZeNyPXq2XiERPacNXyO8dvxW5oShb8VyTgQ0ueNUKgRsQSQ==" crossorigin="anonymous"></script>
|
||||
<!--[if lt IE 10]>
|
||||
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
|
||||
<![endif]-->
|
||||
|
|
|
@ -39,6 +39,7 @@ else:
|
|||
<?php
|
||||
endif;
|
||||
?>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/base-x-3.0.5.1.js" integrity="sha512-tZCra9cpyEtRlLCuglgHQ1pZyX1yZ09NYTTUawN/ptrXkK5I2ndKCWdrswIGGq4fYb2LVcodw3lEzT1UiHlCEg==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/rawdeflate-0.5.js" integrity="sha512-tTdZ7qMr7tt5VQy4iCHu6/aGB12eRwbUy+AEI5rXntfsjcRfBeeqJloMsBU9FrGk1bIYLiuND/FhU42LO1bi0g==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/rawinflate-0.3.js" integrity="sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==" crossorigin="anonymous"></script>
|
||||
<?php
|
||||
|
@ -54,7 +55,7 @@ if ($MARKDOWN):
|
|||
endif;
|
||||
?>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/purify-1.0.7.js" integrity="sha512-VnKJHLosO8z2ojNvWk9BEKYqnhZyWK9rM90FgZUUEp/PRnUqR5OLLKE0a3BkVmn7YgB7LXRrjHgFHQYKd6DAIA==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-tRRKkyV2RYlU1b/PdUmLigAV8mAQpklblVO071AcQs5QbeUwBnK2L5zjbscm2+B2eRJ06luEisXuPowvFHN6fA==" crossorigin="anonymous"></script>
|
||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-zkRNN1TQ1h2agLdyIpjZ7ALamBxeSbmSA1eDUhEZeNyPXq2XiERPacNXyO8dvxW5oShb8VyTgQ0ueNUKgRsQSQ==" crossorigin="anonymous"></script>
|
||||
<!--[if lt IE 10]>
|
||||
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
|
||||
<![endif]-->
|
||||
|
|
Loading…
Reference in New Issue
Block a user