1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-05 02:39:53 +02:00
This commit is contained in:
Paul Lietar 2016-01-02 16:19:39 +01:00
parent 5464647164
commit 90eeed3f80
22 changed files with 380 additions and 342 deletions

View file

@ -6,21 +6,18 @@ use std::ops::Add;
use audio_key::AudioKey;
const AUDIO_AESIV : &'static [u8] = &[
0x72,0xe0,0x67,0xfb,0xdd,0xcb,0xcf,0x77,0xeb,0xe8,0xbc,0x64,0x3f,0x63,0x0d,0x93,
];
const AUDIO_AESIV: &'static [u8] = &[0x72, 0xe0, 0x67, 0xfb, 0xdd, 0xcb, 0xcf, 0x77, 0xeb, 0xe8,
0xbc, 0x64, 0x3f, 0x63, 0x0d, 0x93];
pub struct AudioDecrypt<T : io::Read> {
pub struct AudioDecrypt<T: io::Read> {
cipher: Box<SynchronousStreamCipher + 'static>,
key: AudioKey,
reader: T,
}
impl <T : io::Read> AudioDecrypt<T> {
impl<T: io::Read> AudioDecrypt<T> {
pub fn new(key: AudioKey, reader: T) -> AudioDecrypt<T> {
let cipher = aes::ctr(aes::KeySize::KeySize128,
&key,
AUDIO_AESIV);
let cipher = aes::ctr(aes::KeySize::KeySize128, &key, AUDIO_AESIV);
AudioDecrypt {
cipher: cipher,
key: key,
@ -29,7 +26,7 @@ impl <T : io::Read> AudioDecrypt<T> {
}
}
impl <T : io::Read> io::Read for AudioDecrypt<T> {
impl<T: io::Read> io::Read for AudioDecrypt<T> {
fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
let mut buffer = vec![0u8; output.len()];
let len = try!(self.reader.read(&mut buffer));
@ -40,17 +37,15 @@ impl <T : io::Read> io::Read for AudioDecrypt<T> {
}
}
impl <T : io::Read + io::Seek> io::Seek for AudioDecrypt<T> {
impl<T: io::Read + io::Seek> io::Seek for AudioDecrypt<T> {
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
let newpos = try!(self.reader.seek(pos));
let skip = newpos % 16;
let iv = BigUint::from_bytes_be(AUDIO_AESIV)
.add(BigUint::from_u64(newpos / 16).unwrap())
.to_bytes_be();
self.cipher = aes::ctr(aes::KeySize::KeySize128,
&self.key,
&iv);
.add(BigUint::from_u64(newpos / 16).unwrap())
.to_bytes_be();
self.cipher = aes::ctr(aes::KeySize::KeySize128, &self.key, &iv);
let buf = vec![0u8; skip as usize];
let mut buf2 = vec![0u8; skip as usize];
@ -59,5 +54,3 @@ impl <T : io::Read + io::Seek> io::Seek for AudioDecrypt<T> {
Ok(newpos as u64)
}
}