Save my secrets!

14

1

I have so many secrets and nowhere to keep them!

The goal is simple: write a program that lets you save a string and have it be protected by a password.

The program will take an alias, password and (optional) secret as parameters.

If this is the first time the program is called with a given alias, then it will store/remember the secret, and output whatever you want.

If the program is called with an alias that has been used before, then it will output the secret for that alias iff the password is the same as the first time the program was run with that alias.

Other cases

  • If its the first time being called with a certain alias and no secret is given - store nothing.

  • If the program is called with an alias that has been used, and the password is incorrect - return any kind of error or nothing at all.

  • If the program is called with an alias that has been used, the password is correct and a new secret is supplied - output the old secret and replace it with the new one so that next time only the new secret is output.

  • If the program is called with an alias that has been used, the password is correct and no new secret is supplied - output the old secret and make sure it doesn't get replaced.

Note: These secrets/passwords do not need to be stored securely

Also note: any alphanumeric input for aliases passwords and secrets should be accepted

Standard rules apply, good luck!

Quinn

Posted 2019-05-16T20:54:51.190

Reputation: 1 153

What kind of storage may we use? Anything? – Adám – 2019-05-16T21:00:49.420

May we limit the valid aliases to say lowercase alphabetical words? – Adám – 2019-05-16T21:01:18.520

1Yes, use anything to store it - as for aliases I think it should accept any alphanumeric input – Quinn – 2019-05-16T21:05:15.593

Updated question to specify – Quinn – 2019-05-16T21:08:14.427

1May we use a function rather than a full program? – Arnauld – 2019-05-16T21:16:21.347

1@Arnauld I'll allow it – Quinn – 2019-05-16T21:19:38.430

1May we assume the password and secret are not the empty string? – xnor – 2019-05-17T02:14:47.707

1May we output a null answer (like 0 or None) in any situation where we don't output the secret? – xnor – 2019-05-17T02:37:16.607

1@xnor I will say yes to both those questions – Quinn – 2019-05-17T02:47:18.733

May we output different falsy values (like false and undefined) when there's no secret to output? – Arnauld – 2019-05-20T21:05:51.997

@Arnauld yes you may – Quinn – 2019-05-20T21:13:37.907

Answers

10

JavaScript (ES6),  60  50 bytes

Saved 10 bytes thanks to @JonasWilms!

Takes input as either (alias,password,secret) or (alias,password). Returns undefined the first time a secret is stored, or false if the password is incorrect.

f=(a,p,s,[P,v]=f[a]||[p])=>p==P&&(f[a]=[P,s||v],v)

Try a 1st test case online!

Try a 2nd test case online!

How?

We define a named function \$f\$ whose underlying object is also used to store the passwords and the secrets.

Commented

f = (             // f = named function whose underlying object is used for storage
  a,              // a = alias
  p,              // p = password
  s,              // s = optional secret
  [P, v] = f[a]   // if f[a] is defined, retrieve the password P and the secret v
           || [p] // otherwise, copy p in P
) =>              //
  p == P && (     // if p is not equal to P, yield false; else:
    f[a] = [      //   update f[a]:
      P,          //     save the new password (or leave it unchanged)
      s || v      //     save the new secret if it's defined, or keep the previous one
    ],            //
    v             //   return the previous secret
  )               //

Arnauld

Posted 2019-05-16T20:54:51.190

Reputation: 111 334

Took me a second to spot how you were storing things! – Shaggy – 2019-05-16T21:53:23.753

hmm, is this correct?

– ngn – 2019-05-17T05:35:57.350

1@ngn It's most likely incorrect indeed. Thanks for reporting this. Should be fixed now. – Arnauld – 2019-05-17T07:16:46.747

-7 bytes through storing a [password, value] array inside of the object. – Jonas Wilms – 2019-05-20T20:52:49.810

@JonasWilms Nicely done! I can't think of any situation where p&& is required. So, that's another -3 bytes. – Arnauld – 2019-05-20T21:42:58.547

@arnauld ah right, missread one of the cases. Thanks :) – Jonas Wilms – 2019-05-21T07:58:58.443

6

Python 2, 94 93 bytes

def f(a,p,s=0,d={}):
 q,t=d.get(a,(0,0))
 if q==p:d[a]=p,s or t;return t
 elif q<1<s:d[a]=p,s

Try it online!

For once, Python's weird default dict parameter works in my favor...

Chas Brown

Posted 2019-05-16T20:54:51.190

Reputation: 8 959

Wait, using a default argument makes it just one object instead of a new one on function call? Hmmm... This explains a lot of debugging I've had to do previously lol. – HyperNeutrino – 2019-05-23T01:46:39.130

3

Ruby, 64 bytes

Builds a hash for aliases to a single key-pair of password => secret. Probably could be more elegant.

->a,w,s=p{@q||={};(b=@q[a])?s&&b[w]?b[w]=s:b[w]:s&&@q[a]={w=>s}}

Try it online!

Value Ink

Posted 2019-05-16T20:54:51.190

Reputation: 10 608

2

Wolfram Language (Mathematica), 34 bytes

(c=#~g~#2;#3!=##3||(#~g~#2=#3);c)&

Try it online!

attinat

Posted 2019-05-16T20:54:51.190

Reputation: 3 495

1

C# (Visual C# Interactive Compiler), 140 138 134 bytes

-2 bytes thanks to @Expired Data

a=>p=>s=>(a=P.ContainsKey(a)?P[a]:P[a]=new[]{p,s},o:p==a[0]?a[1]:p,s==""?s:p==a[0]?a[1]=s:s).o;var P=new Dictionary<string,string[]>()

Try it online!

my pronoun is monicareinstate

Posted 2019-05-16T20:54:51.190

Reputation: 3 111

1138 bytes – Expired Data – 2019-05-17T15:35:39.337

1@ExpiredData 134, as P doesn't have to be dynamic now – my pronoun is monicareinstate – 2019-05-17T15:50:19.887

130 bytes using the same trick again – Expired Data – 2019-05-17T15:59:25.053

0

Python 2, 77 bytes

def f(a,p,s=0,d={}):
 P,S=d.get(a,(p,0))
 if p==P:
	if s>0:d[a]=p,s
	return S

Try it online!

Similar to Chas Brown's method.

xnor

Posted 2019-05-16T20:54:51.190

Reputation: 115 687