tonistiigi / mega

THIS REPO IS NOT MAINTAINED. Unofficial Node.js SDK for Mega
http://tonistiigi.github.com/mega
118 stars 33 forks source link

folder listing #19

Closed smolleyes closed 10 years ago

smolleyes commented 10 years ago

hi

how can i get files list for a directory ?

exemple:

https://mega.co.nz/#F!Ys1mFJYL

the mega.file function just return a download id no childrens array

thanks :)

tonistiigi commented 10 years ago

Opening by link only works for files at the moment.

smolleyes commented 10 years ago

hi

ok thanks

np just found the posts to get the file listing and download link for each file...

jsut don t know how i can decode the filename

if i have an object like this

a: "wmLCv-qrn1ZKgv72ypPj-VxK26KJlo4kvBGnmTmbJiXrWlvvW0X8xBqLiT3XL11vFaVIbV8DifPXt-KtPYwAaA" h: "yIpWlSxJ" k: "jAh1laAY:IepeDIY6k4LT6PRI2BfxbYzLRNuRugNX8i4DnPpkRos" p: "jAh1laAY" s: 14848128764 t: 0 ts: 1395386169 u: "91vQIy1UZ14"

how can i convert to to "normal " mega file download link or just decrypt the filename ?

if you have an idear ...

thanks

tonistiigi commented 10 years ago

"a" property should contain all the metadata attributes. Its AES-CBC encrypted with zero iv.

If you use a link the key for encrypting is just included there. In your current case the "k" property contains file id+encrypted key. This first needs to be decrypted with your master key. https://github.com/tonistiigi/mega/blob/master/lib/crypto/index.js#L105

smolleyes commented 10 years ago

hi

thanks a lot for informatons

now i can t understand how to do with your package :( i m feeling stupid lol

crypto.Decipheriv('aes-128-cbc','????',0)

like that ?

don t understand neither for the k property you explained

sorry

thx

smolleyes commented 10 years ago

so

i can download the file but can't decrypt it ... don t know where is the master key you speak about i have the main links #!F from a forum so i need to ask the posters their key to derypt their files or i can use another way to decrypt it ?

thanks

tonistiigi commented 10 years ago

Master key is the user key.

Every file is encrypted with its own key. Then this key is also encrypted and stored with the file(this is the k property). Key can be encrypted with either user's master key or with the key of some parent folder(this is the part until colon).

Attributes objects are encrypted with AES-CBC-128, if the key is 256bit, its XORed to 128bit first. Keys are encrypted with AES-ECB-128.

When you make a link then the last part of the link is unencrypted key to that resource. So if its a file then you can just use that to decrypt the attributes. For a folder you have to first use this key to decrypt the folder key. Then with the folder key you can decrypt all the file keys that use this folder key.

smolleyes commented 10 years ago

hello thanks for your help, i begin to understand...

so for exemple, if i have

folder link (my file and my created sound): https://mega.co.nz/#F!bAFSxAAL!AOUU-DEgRdg4Khd4zM7ndA

with my post for listing i have

[Object] 0: Object f: Array[2] 0: Object a: "vFE7R9rzPJs7fu4GmFHnxQ" h: "GJclVaKI" k: "GJclVaKI:LHUN3ZQJZqN_TN8IKAAh4Q" p: "OM8xAaBY" t: 1 ts: 1395667487 u: "65fYYu5ZLBU" proto: Object

the dir is object 0 and the key is LHUN3ZQJZqN_TN8IKAAh4Q

so i first need to decode this key LHUN3ZQJZqN_TN8IKAAh4Q with the key in my main link ? (AOUU-DEgRdg4Khd4zM7ndA)

then i ll use the result to decode all the files that s it ?

tonistiigi commented 10 years ago

Please do not link to content you don't own in this repo. This kind of stuff is not allowed in here.

smolleyes commented 10 years ago

sorry i changed it all :)

my "logic" is right ?

tonistiigi commented 10 years ago
function d64(s) {
  s += '=='.substr((2-s.length*3)&3)
  s = s.replace(/\-/g,'+').replace(/_/g,'/').replace(/,/g,'')
  return new Buffer(s, 'base64')
}

var k0 = d64('AOUU-DEgRdg4Khd4zM7ndA')
var a = d64('vFE7R9rzPJs7fu4GmFHnxQ')
var k = d64('LHUN3ZQJZqN_TN8IKAAh4Q')

var crypto = require('crypto')
var iv = Buffer(16)
iv.fill(0)

var aes = crypto.createDecipheriv('aes-128-ecb', k0, Buffer(0))
aes.setAutoPadding(false)
var kdec = aes.update(k)

aes = crypto.createDecipheriv('aes-128-cbc', kdec, iv)
aes.setAutoPadding(false)
console.log(aes.update(a).toString())

// aes = crypto.createDecipheriv('aes-128-ecb', k0, Buffer(0))
// aes.setAutoPadding(false)
// var k2dec = aes.update(k2)
smolleyes commented 10 years ago

thank you for the exemple (i have some works with this type of things... :( )

i try to decode the filename with your exemple now... no success for the moment lol

smolleyes commented 10 years ago

obj is

1: Object a: "p91p4G1aF4DSOUajkxGgTuJDBnGqXIa4XBNSkJu1zwkWjhS8iCZmdoCRWDX3ebmBDemU_VFYvoAGTq8mb6LtuQ" h: "zR8niALR" k: "GJclVaKI:vhe23fV90E6We7hrSzoXtZYafjBwjqYSQcAZOlrQGLo" p: "GJclVaKI" s: 2529042 t: 0 ts: 1395678307 u: "65fYYu5ZLBU"

you told me i have to use the decoded folder key to decrypt the files attributes, so i take the kdec of your exemple to decode ?

thnaks again for your exemple :+1:

smolleyes commented 10 years ago

hello

so... i tried

function d64(s) { s += '=='.substr((2-s.length*3)&3) s = s.replace(/-/g,'+').replace(/_/g,'/').replace(/,/g,'') return new Buffer(s, 'base64') }

//dir var k0 = d64('AOUU-DEgRdg4Khd4zM7ndA') var a = d64('vFE7R9rzPJs7fu4GmFHnxQ') var k = d64('LHUN3ZQJZqN_TN8IKAAh4Q')

//file 1 var a2 = d64('p91p4G1aF4DSOUajkxGgTuJDBnGqXIa4XBNSkJu1zwkWjhS8iCZmdoCRWDX3ebmBDemU_VFYvoAGTq8mb6LtuQ') var k2 = d64('vhe23fV90E6We7hrSzoXtZYafjBwjqYSQcAZOlrQGLo')

var node_crypto = require('crypto') var iv = Buffer(16) iv.fill(0)

// decrypt folder var aes = node_crypto.createDecipheriv('aes-128-ecb', k0, Buffer(0)) aes.setAutoPadding(false) var kdec = aes.update(k)

aes = node_crypto.createDecipheriv('aes-128-cbc', kdec, iv) aes.setAutoPadding(false) console.log(aes.update(a).toString())

//// decrypt file aes = node_crypto.createDecipheriv('aes-128-ecb', k0, Buffer(0)) // i tried with kdec var aes.setAutoPadding(false) var k2dec = aes.update(k2)

aes = node_crypto.createDecipheriv('aes-128-cbc', k2dec, iv) aes.setAutoPadding(false) console.log(aes.update(a2).toString())

return error

crypto.js:351 this._binding.initiv(cipher, toBuf(key), toBuf(iv)); ^ Error: Invalid key length at new Decipheriv (crypto.js:351:17) at Object.Decipheriv (crypto.js:348:12) at Object. (/tmp/dec.js:34:19) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:349:32) at Function.Module._load (module.js:305:12) at Function.Module.runMain (module.js:490:10) at startup (node.js:123:16) at node.js:1027:3

:(

tonistiigi commented 10 years ago

if the key is 256bit, its XORed to 128bit first.

xor first 128bits with next 128bits before using k2dec in cbc

smolleyes commented 10 years ago

hello

lol i m sorry it s not usual for me to works with things like that ... bits and co

i never used xor even don t know what it is

i see with google it s a simple ^ to add, but add to what ?

thanks again and sorry for my inefficiency on this ... :/

tonistiigi commented 10 years ago
function from256to128(s) {
  var o = new Buffer(16)
  for (var i = 0; i < 16; i++) {
    o[i] = s[i] ^ s[i + 16]
  }
  return o
}
smolleyes commented 10 years ago

hi

thanks it works perfectly :)

a last question ... is it possible to get the key (k2dec) as a string to pass it to your mega.decrypt function ?

THANKS a lot !

tonistiigi commented 10 years ago

mega.decrypt() does not need strings as key input. It takes in Buffers so you don't need to convert anything. Remember that this time it needs the whole 256bits.

smolleyes commented 10 years ago

hello

ok i m trying many things to do this since i still don t really understand :p

small question to not work for nothing... i get my download link with (for my exemple)

$.post('https://eu.api.mega.co.nz/cs?id=1&n=bAFSxAAL','[{"a":"g","g":1,"ssl":1,"n":"zR8niALR"}]').done(function(res) {console.log("link: "+ res[0].g+"\nsize:" + res[0].s)})

i get

link: https://gfs270n053.userstorage.mega.co.nz/dl/UrHc4x9PsLtZRgLPUWUJMpj8QISJHC…3aqVJg0bnKU1trc-brL6VD0Mkrfh0wNNZBpkDXPHnmS0j4xxNshXDR6oFL4OTxPqOdSUYsEvxw

size: 2529042

wil the 256 bit decoded key i try to get will be the same to decode this link or mega give another key for this type of link ?

thx :)

tonistiigi commented 10 years ago

AFAIK every file only has one key. When you link to a file it is plaintext included in the URL. When you link to a folder its the k property decrypted with folder key. But its the same in the end. Its the same k2dec from the previous example.

smolleyes commented 10 years ago

hello

ok thanks, i spend houuuuurs trying to get this full 256b as a normal string (and not string neither) but i m tooooo bad with crypto :_(

completely lost ...

++

smolleyes commented 10 years ago

hi toni

sorry to ask you and shame on me but could you show me an exemple on howto get this damn 256bits key as string please, not usual for me to ask like that but i tried a lot and can t get it successfully ... :( ?

thanks

tonistiigi commented 10 years ago

As I said before you don't need it as string. If you want encode mega style base64 then use this e64 function https://github.com/tonistiigi/mega/blob/master/lib/crypto/index.js#L12

smolleyes commented 10 years ago

hi toni

thanks a lot everything works right now :)

another problem i have is that i can t stop a download, i have a network monitor and even if i call end() or destroy() on the mega.download() stream it do not stop the download and my monitor still show a download running

lastest question .. :p is it possible to add a seeking like function to this stream ?

thanks