parent
5497c33e5c
commit
28ef384bfe
@ -0,0 +1,41 @@ |
||||
import { |
||||
compress, |
||||
decompress |
||||
} from './public-key'; |
||||
|
||||
export function stringify(cipher) { |
||||
if (typeof cipher === 'string') return cipher; |
||||
|
||||
// use compressed key because it's smaller
|
||||
const compressedKey = compress(cipher.ephemPublicKey); |
||||
|
||||
const ret = Buffer.concat([ |
||||
new Buffer(cipher.iv, 'hex'), // 16bit
|
||||
new Buffer(compressedKey, 'hex'), // 33bit
|
||||
new Buffer(cipher.mac, 'hex'), // 32bit
|
||||
new Buffer(cipher.ciphertext, 'hex') // var bit
|
||||
]); |
||||
|
||||
return ret.toString('hex'); |
||||
|
||||
|
||||
} |
||||
|
||||
export function parse(str) { |
||||
if (typeof str !== 'string') |
||||
return str; |
||||
|
||||
const buf = new Buffer(str, 'hex'); |
||||
|
||||
const ret = { |
||||
iv: buf.toString('hex', 0, 16), |
||||
ephemPublicKey: buf.toString('hex', 16, 49), |
||||
mac: buf.toString('hex', 49, 81), |
||||
ciphertext: buf.toString('hex', 81, buf.length) |
||||
}; |
||||
|
||||
// decompress publicKey
|
||||
ret.ephemPublicKey = '04' + decompress(ret.ephemPublicKey); |
||||
|
||||
return ret; |
||||
} |
Loading…
Reference in new issue