Skip to content

Commit

Permalink
add sha256 function for all targets
Browse files Browse the repository at this point in the history
  • Loading branch information
bunopnu committed Oct 20, 2023
1 parent cf537ee commit e1e6960
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"[javascript]": {
"editor.formatOnPaste": false,
"editor.formatOnSave": false,
"editor.formatOnType": false
},
"[erlang]": {
"editor.formatOnPaste": false,
"editor.formatOnSave": false,
"editor.formatOnType": false
}
}
11 changes: 11 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "gleam_stdlib", version = "0.31.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6D1BC5B4D4179B9FEE866B1E69FE180AC2CE485AD90047C0B32B2CA984052736" },
{ name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
]

[requirements]
gleam_stdlib = { version = "~> 0.31" }
gleeunit = { version = "~> 0.10" }
6 changes: 5 additions & 1 deletion src/glevatar.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import gleam/io

@external(erlang, "sha256", "sha256")
@external(javascript, "./sha256.mjs", "sha256")
fn sha256(input: String) -> String

pub fn main() {
io.println("Hello from glevatar!")
io.println(sha256("trying out"))
}
8 changes: 8 additions & 0 deletions src/sha256.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-module(sha256).

-export([sha256/1]).

-spec sha256(binary()) -> binary().
sha256(Input) ->
<<Hash:256/big-unsigned-integer>> = crypto:hash(sha256, Input),
lists:flatten(io_lib:format("~64.16.0b", [Hash])).
9 changes: 9 additions & 0 deletions src/sha256.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
function B(t,...r){if(!(t instanceof Uint8Array))throw new Error("Expected Uint8Array");if(r.length>0&&!r.includes(t.length))throw new Error(`Expected Uint8Array of length ${r}, not of length=${t.length}`)}function T(t,r=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(r&&t.finished)throw new Error("Hash#digest() has already been called")}function I(t,r){B(t);let e=r.outputLen;if(t.length<e)throw new Error(`digestInto() expects output buffer of length at least ${e}`)}var O=t=>t instanceof Uint8Array;var w=t=>new DataView(t.buffer,t.byteOffset,t.byteLength),b=(t,r)=>t<<32-r|t>>>r,C=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;if(!C)throw new Error("Non little-endian hardware is not supported");function k(t){if(typeof t!="string")throw new Error(`utf8ToBytes expected string, got ${typeof t}`);return new Uint8Array(new TextEncoder().encode(t))}function E(t){if(typeof t=="string"&&(t=k(t)),!O(t))throw new Error(`expected Uint8Array, got ${typeof t}`);return t}var m=class{clone(){return this._cloneInto()}},S={}.toString;function L(t){let r=c=>t().update(E(c)).digest(),e=t();return r.outputLen=e.outputLen,r.blockLen=e.blockLen,r.create=()=>t(),r}function D(t,r,e,c){if(typeof t.setBigUint64=="function")return t.setBigUint64(r,e,c);let s=BigInt(32),o=BigInt(4294967295),u=Number(e>>s&o),n=Number(e&o),a=c?4:0,f=c?0:4;t.setUint32(r+a,u,c),t.setUint32(r+f,n,c)}var g=class extends m{constructor(e,c,s,o){super();this.blockLen=e;this.outputLen=c;this.padOffset=s;this.isLE=o;this.finished=!1;this.length=0;this.pos=0;this.destroyed=!1;this.buffer=new Uint8Array(e),this.view=w(this.buffer)}update(e){T(this);let{view:c,buffer:s,blockLen:o}=this;e=E(e);let u=e.length;for(let n=0;n<u;){let a=Math.min(o-this.pos,u-n);if(a===o){let f=w(e);for(;o<=u-n;n+=o)this.process(f,n);continue}s.set(e.subarray(n,n+a),this.pos),this.pos+=a,n+=a,this.pos===o&&(this.process(c,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){T(this),I(e,this),this.finished=!0;let{buffer:c,view:s,blockLen:o,isLE:u}=this,{pos:n}=this;c[n++]=128,this.buffer.subarray(n).fill(0),this.padOffset>o-n&&(this.process(s,0),n=0);for(let i=n;i<o;i++)c[i]=0;D(s,o-8,BigInt(this.length*8),u),this.process(s,0);let a=w(e),f=this.outputLen;if(f%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let p=f/4,d=this.get();if(p>d.length)throw new Error("_sha2: outputLen bigger than state");for(let i=0;i<p;i++)a.setUint32(4*i,d[i],u)}digest(){let{buffer:e,outputLen:c}=this;this.digestInto(e);let s=e.slice(0,c);return this.destroy(),s}_cloneInto(e){e||=new this.constructor,e.set(...this.get());let{blockLen:c,buffer:s,length:o,finished:u,destroyed:n,pos:a}=this;return e.length=o,e.pos=a,e.finished=u,e.destroyed=n,o%c&&e.buffer.set(s),e}};var F=(t,r,e)=>t&r^~t&e,V=(t,r,e)=>t&r^t&e^r&e,j=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),x=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),h=new Uint32Array(64),A=class extends g{constructor(){super(64,32,8,!1);this.A=x[0]|0;this.B=x[1]|0;this.C=x[2]|0;this.D=x[3]|0;this.E=x[4]|0;this.F=x[5]|0;this.G=x[6]|0;this.H=x[7]|0}get(){let{A:e,B:c,C:s,D:o,E:u,F:n,G:a,H:f}=this;return[e,c,s,o,u,n,a,f]}set(e,c,s,o,u,n,a,f){this.A=e|0,this.B=c|0,this.C=s|0,this.D=o|0,this.E=u|0,this.F=n|0,this.G=a|0,this.H=f|0}process(e,c){for(let i=0;i<16;i++,c+=4)h[i]=e.getUint32(c,!1);for(let i=16;i<64;i++){let l=h[i-15],y=h[i-2],_=b(l,7)^b(l,18)^l>>>3,U=b(y,17)^b(y,19)^y>>>10;h[i]=U+h[i-7]+_+h[i-16]|0}let{A:s,B:o,C:u,D:n,E:a,F:f,G:p,H:d}=this;for(let i=0;i<64;i++){let l=b(a,6)^b(a,11)^b(a,25),y=d+l+F(a,f,p)+j[i]+h[i]|0,U=(b(s,2)^b(s,13)^b(s,22))+V(s,o,u)|0;d=p,p=f,f=a,a=n+y|0,n=u,u=o,o=s,s=y+U|0}s=s+this.A|0,o=o+this.B|0,u=u+this.C|0,n=n+this.D|0,a=a+this.E|0,f=f+this.F|0,p=p+this.G|0,d=d+this.H|0,this.set(s,o,u,n,a,f,p,d)}roundClean(){h.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}},H=class extends A{constructor(){super();this.A=-1056596264;this.B=914150663;this.C=812702999;this.D=-150054599;this.E=-4191439;this.F=1750603025;this.G=1694076839;this.H=-1090891868;this.outputLen=28}},K=L(()=>new A),q=L(()=>new H);

/*! edited part of the source code */
export function sha256(input) {
return [...K(input)]
.map(x => x.toString(16).padStart(2, '0'))
.join('');
}

0 comments on commit e1e6960

Please sign in to comment.