Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

folder listing #19

Closed
smolleyes opened this issue Mar 24, 2014 · 24 comments
Closed

folder listing #19

smolleyes opened this issue Mar 24, 2014 · 24 comments

Comments

@smolleyes
Copy link

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
Copy link
Owner

Opening by link only works for files at the moment.

@smolleyes
Copy link
Author

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
Copy link
Owner

"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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

@smolleyes
Copy link
Author

sorry i changed it all :)

my "logic" is right ?

@tonistiigi
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

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 👍

@smolleyes
Copy link
Author

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
Copy link
Owner

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

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

@smolleyes
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants